gpt4 book ai didi

java - 重复链表值

转载 作者:行者123 更新时间:2023-12-01 11:38:36 25 4
gpt4 key购买 nike

我有一个链接列表,如果我每次插入一个对象,那么最后一个值就会重复。不是第一次插入,而是下一次插入,它重复1,6,16....请检查下面的代码

Java 代码:

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

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
double ID =Double.parseDouble(request.getParameter("value1"));
double age=Double.parseDouble(request.getParameter("value2"));
String type=request.getParameter("value5");
int pre=Integer.parseInt(request.getParameter("value3"));
int nxt=Integer.parseInt(request.getParameter("value4"));
String config_Feature=request.getParameter("value6");

try {
if(config_Feature.equals("insert")) {
int insertAtIndexForJSONArray = findInsertAtIndex(linkedList,nxt);

insertValue(insertAtIndexForJSONArray,linkedList,ID,age,type);

JSONArray alist = new JSONArray(linkedList);
System.out.println(alist);
WritingJsonDataToFile(alist);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}

private static int findInsertAtIndex(List<JSONObject> linkedList2, int i) throws JSONException {
int curIndex =0;
int reqIndex=0;
for(int j=0;j<linkedList2.size();j++){
JSONObject jo = (JSONObject) linkedList2.get(j);
if(jo.getString("type").equalsIgnoreCase("s")){
if(curIndex==i){
reqIndex=j;
break;
}
curIndex++;
}
}
return reqIndex;
}

private static void insertValue(int index, List<JSONObject> linkedList2,double ID,double age,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<linkedList2.size();i++) {
ja1.put(linkedList2.get(i));
}
JSONObject jo = new JSONObject();
JSONObject jo3 = new JSONObject();
jo.put("ID", ID);
jo.put("Age", age);
jo3.put("type", t);
jo3.put("DB", jo);
linkedList2.add(index, jo3);

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

第一次插入的 O/P

[{"DB":{"ID":1239,"Age":55},"type":"L"}]
For 2nd insertion
[{"DB":{"ID":1233,"Age":45},"type":"L"},{"DB":{"ID":1239,"Age":55},"type":"L"},{"DB":{"ID":1239,"Age":55},"type":"L"}]
For 3rd insertion
[{"DB":{"ID":1233,"Age":45},"type":"L"},{"DB":{"ID":10010,"Age":22},"type":"s"},{"DB":{"ID":1239,"Age":55},"type":"L"},{"DB":{"ID":1239,"Age":55},"type":"L"},{"DB":{"ID":1239,"Age":55},"type":"L"},{"DB":{"ID":1239,"Age":55},"type":"L"}]

最佳答案

insertValue() ,首先,将链表元素复制到 json 数组中,然后将新元素添加到链表中,然后再次添加首先复制的元素...这些元素已经在链表中。

一个LinkedList为您处理元素的插入(这就是使用集合的目的)。您不需要移动任何东西或复制自己。

为了说明,在LinkedList<String>上,

// if linkedlist contains ["a", "b", "s"]
linkedlist.add(2, "X");
// linkedlist contains now ["a", "b", "X", "c"]

该方法应该类似于(我还没有编译它):

private static void insertValue(int index, List<JSONObject> linkedList2,
double ID, double age, String t) throws JSONException {
JSONObject jo1 = new JSONObject();
jo1.put("ID", ID);
jo1.put("Age", age);
JSONObject jo2 = new JSONObject();
jo2.put("type", t);
jo2.put("DB", jo1);
linkedList2.add(index, jo2);
}

关于java - 重复链表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29741412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com