将新元素插入链接列表

时间:2015-04-09 08:14:11

标签: java linked-list

我的HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="JsonData" method="post">
<br>
  Previous stop: <input type="text" name="pre"><br>
  Next stop: <input type="text" name="nxt"><br>
  Latitude: <input type="text" name="latitude"><br>
  Longitude: <input type="text" name="longitude"><br>
  Type: <input type="text" name="type"><br>

  <input type="submit" value="Submit">
</form>
</body>
</html>

Servlet代码:

public class InsertPos extends HttpServlet
{

    private static final long serialVersionUID = 1L;


     List<JSONObject> linkedList = new LinkedList<JSONObject>();

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {

            response.setContentType("text/html;charset=UTF-8");
            double lat =Double.parseDouble(request.getParameter("latitude"));
            double lon =Double.parseDouble(request.getParameter("longitude"));
            String type=request.getParameter("type");
            int pre=Integer.parseInt(request.getParameter("pre"));
            int nxt=Integer.parseInt(request.getParameter("nxt"));

            JSONObject obj1=new JSONObject();



        try 
        {
            if(nxt==999&&type.equals("v"))
            {
                System.out.println("Inserting Object Before a Via["type":"v"]" );
                obj1.put("lat", lat);
                obj1.put("lon", lon);
                obj1.put("type", type);
                int i=pre+1;
                linkedList.add(i, obj1);

                JSONArray alsit = new JSONArray(linkedList);
                System.out.println(alsit);

            }
            else if(pre==999&&type.equals("s"))
            {   
                System.out.println("Inserting Object After a Via["type":"v"]" );
                JSONArray alsit = new JSONArray(linkedList);
                int insertAtIndexForJSONArray = findInsertAtIndex(alsit,nxt);

                insertValue(insertAtIndexForJSONArray,alsit,lat,lon,type);


                System.out.println("New JSON Array :"+alsit);
            }
            else 
            {
                obj1.put("lat", lat);
                obj1.put("lon", lon);
                obj1.put("type", type);
                linkedList.add(nxt, obj1);

                JSONArray alsit = new JSONArray(linkedList);
                System.out.println(alsit);
            }

        }

        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        }


        private static int findInsertAtIndex(JSONArray ja, int i) throws JSONException 
        {

            int curIndex =0;
            int reqIndex=0;
            for(int j=0;j<ja.length();j++){
                JSONObject jo = ja.getJSONObject(j);;
                if(jo.getString("type").equalsIgnoreCase("s")){
                     if(curIndex==i){
                            reqIndex=j;
                            break;
                        }
                        curIndex++;
                }
            }

            return reqIndex;
        }
        private static void insertValue(int index, JSONArray ja,double lat,double lon,String t) throws JSONException 
        {

             JSONArray ja1= new JSONArray();
            //Copy the data from that index in another JSONArray
            //It is an array, so you will need to do shifting
            for(int i=index;i<ja.length();i++){
                ja1.put(ja.get(i));
            }
            JSONObject jo = new JSONObject();
            jo.put("lat", lat);
            jo.put("lon", lon);
            jo.put("type", t);
            ja.put(index, jo);;

            int shiftIndex = index+1;
            for(int i=0;i<ja1.length();i++){
                ja.put(shiftIndex,ja1.get(i));
                shiftIndex++;
            }


        }


}

如果我第一次跑,将提供pre&amp; nxt分别为0,0,所以现在执行else部分-at 0th index插入     [{“lon”:77.57200874383307,“type”:“s”,“lat”:12.905683932781095}]

第二次将pre&amp; nxt分别设为0,1所以再次执行其他部分。     [{ “LON”:77.46553577478217, “类型”: “S”, “LAT”:12.894117321535727},{ “LON”:77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095}]

第三次将插入“v”b / w 0&amp; 1,所以再次执行其他部分   [{ “LON”:77.46553577478217, “类型”: “S”, “LAT”:12.894117321535727},{ “LON”:77.54596985871751, “类型”: “V”, “LAT”:12.878199284620463},{ “LON”: 77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095}]

现在我再次插入“v”b / w 0&amp; 1,这里索引1包含“type”:“v”,因此将提供pre为0&amp; nxt为999,下一个点是via,if(nxt == 999&amp;&amp; type.equals(“v”))被执行。

[{ “LON”:77.46553577478217, “类型”: “S”, “LAT”:12.894117321535727},{ “LON”:77.56673430067228, “类型”: “V”, “LAT”:12.893814030683414},{” LON “:77.54596985871751,” 类型 “:” v”, “LAT”:12.878199284620463},{ “LON”:77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095}]

现在我在最后一点之前插入一个点,用户只考虑“s”&amp;提供pre为999(其为“v”)&amp; nxt为1,在我们的代码中,如果(pre == 999&amp;&amp; type.equals(“s”))

,则执行此部分。

新JSON数组:[{“lon”:77.46553577478217,“type”:“s”,“lat”:12.894117321535727},{“lon”:77.56673430067228,“type”:“v”,“lat”:12.893814030683414 },{ “LON”:77.54596985871751, “类型”: “v”, “LAT”:12.878199284620463},{ “LON”:77.5358620557242, “类型”: “S”, “LAT”:12.900077980951266},{ “LON” :77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095}] 成功插入

假设我在新插入点之后添加另一个点,在这种情况下,如果我在第三个索引之后添加,则将索引提供为1&amp; 2用户不会考虑“v”

当前o / p:
[{ “LON”:77.46553577478217, “类型”: “S”, “LAT”:12.894117321535727},{ “LON”:77.56673430067228, “类型”: “V”, “LAT”:12.893814030683414},{ “LON”: 77.48705658049113, “类型”: “S”, “LAT”:12.892861822540487},{ “LON”:77.54596985871751, “类型”: “v”, “LAT”:12.878199284620463},{ “LON”:77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095}]

我面临的问题是 最后插入的数据正在被删除,因为你可以看到int o / p第三个索引是覆盖d&amp;新数据也将插入索引2,应插入第4个索引

预期退出/放置
[{ “LON”:77.46553577478217, “类型”: “S”, “LAT”:12.894117321535727},{ “LON”:77.56673430067228, “类型”: “V”, “LAT”:12.893814030683414},{ “LON”: 77.54596985871751, “类型”: “v”, “LAT”:12.878199284620463},{ “LON”:77.5358620557242, “类型”: “S”, “LAT”:12.900077980951266} 的 { “LON”:77.48705658049113, “类型”: “S”, “LAT”:12.892861822540487}, { “LON”:77.57200874383307, “类型”: “S”, “LAT”:12.905683932781095} ]

1 个答案:

答案 0 :(得分:0)

在评论中提及Jon时,您应该提及要插入的位置,i方法的jinsertStop。如果要在特定索引处插入,则必须使用add方法。

public class Test {

    public static void main(String[] args) {
        List<String> list = new LinkedList<String>();
        // add elements to the linked list
        list.add("F:1");//0
        list.add("B:v");
        list.add("D:v");
        list.add("E:v");
        list.add("C:1");//1
        list.add("M:x");
        list.add("N:1");//2
        list.add("K:y");
        list.add("L:s");
        list.add("O:1");//3
        System.out.println("Old List :"+list);

        int insertAtIndex=findInsertAtIndex(list,1,2);
        //Here I do not know, what are i and j
        //I can assume that  between 2 consecutive you will need to insert
        //What is the use of giving 1st index then
        //This is not clear here
        insertValue(insertAtIndex,list);
        System.out.println("New List After Inserting :"+list);

   }

    private static int findInsertAtIndex(List<String> list,int i,int j) {
        int curIndex =0;
        int reqIndex=0;
        for(int k=0;k<list.size();k++){
            if(list.get(k).endsWith(":1")){
                if(curIndex==j){
                    reqIndex=k;
                    break;
                }
                curIndex++;

            }
        }
        return reqIndex;
    }
   private static void insertValue(int insertIndex, List<String> list) {
       list.add(insertIndex, "Z:1");

   }

}

<强>输出

Old List :[F:1, B:v, D:v, E:v, C:1, M:x, N:1, K:y, L:s, O:1]
New List After Inserting :[F:1, B:v, D:v, E:v, C:1, M:x, Z:1, N:1, K:y, L:s, O:1]

编辑:为JSONArray添加了代码 JSONArray is **not** a LINKEDLIST

public class Test {

    public static void main(String[] args) throws JSONException {


        JSONArray ja = new JSONArray();

        JSONObject jo = new JSONObject();
        jo.put("lon", "F");
        jo.put("type", "1");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "B");
        jo.put("type", "v");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "D");
        jo.put("type", "v");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "E");
        jo.put("type", "v");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "C");
        jo.put("type", "1");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "M");
        jo.put("type", "x");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "N");
        jo.put("type", "1");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "K");
        jo.put("type", "y");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "L");
        jo.put("type", "s");
        ja.put(jo);

        jo = new JSONObject();
        jo.put("lon", "O");
        jo.put("type", "1");
        ja.put(jo);

        System.out.println("Old JSON Array :"+ja);

        int insertAtIndexForJSONArray = findInsertAtIndex(ja,2);

        insertValue(insertAtIndexForJSONArray,ja);


        System.out.println("New JSON Array :"+ja);

   }

    private static void insertValue(int index, JSONArray ja) throws JSONException {

        JSONArray ja1= new JSONArray();
        //Copy the data from that index in another JSONArray
        //It is an array, so you will need to do shifting
        for(int i=index;i<ja.length();i++){
            ja1.put(ja.get(i));
        }
        JSONObject jo = new JSONObject();
        jo.put("lon", "Z");
        jo.put("type", "1");
        ja.put(index, jo);

        int shiftIndex = index+1;
        for(int i=0;i<ja1.length();i++){
            ja.put(shiftIndex,ja1.get(i));
            shiftIndex++;
        }


    }

    private static int findInsertAtIndex(JSONArray ja, int i) throws JSONException {

        int curIndex =0;
        int reqIndex=0;
        for(int j=0;j<ja.length();j++){
            JSONObject jo = ja.getJSONObject(j);
            if(jo.getString("type").equalsIgnoreCase("1")){
                 if(curIndex==i){
                        reqIndex=j;
                        break;
                    }
                    curIndex++;
            }
        }

        return reqIndex;
    }

}

<强>输出

Old JSON Array :[{"lon":"F","type":"1"},{"lon":"B","type":"v"},{"lon":"D","type":"v"},{"lon":"E","type":"v"},{"lon":"C","type":"1"},{"lon":"M","type":"x"},{"lon":"N","type":"1"},{"lon":"K","type":"y"},{"lon":"L","type":"s"},{"lon":"O","type":"1"}]
New JSON Array :[{"lon":"F","type":"1"},{"lon":"B","type":"v"},{"lon":"D","type":"v"},{"lon":"E","type":"v"},{"lon":"C","type":"1"},{"lon":"M","type":"x"},{"lon":"Z","type":"1"},{"lon":"N","type":"1"},{"lon":"K","type":"y"},{"lon":"L","type":"s"},{"lon":"O","type":"1"}]

注意:我使用的是Jettison JSONArray