我有一个链接列表,如果我每次插入一个对象,那么最后一个值是重复的。不是第一个插入,但是对于下一次插入,它重复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"}]
答案 0 :(得分:1)
在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);
}