重复链表值
Repeating linked list value
我有一个链表,如果我每次都插入一个对象,那么第一次插入的最后一个值是 repeating.Not,但对于下一次插入,它会重复 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数组中,然后将新元素添加到链表中,然后再次 你添加第一个复制的元素......它们已经在链表中。
A LinkedList
为您处理元素的插入(这就是使用集合的目的)。您不需要 shift 任何东西或复制您自己。
为了便于说明,在 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);
}
我有一个链表,如果我每次都插入一个对象,那么第一次插入的最后一个值是 repeating.Not,但对于下一次插入,它会重复 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数组中,然后将新元素添加到链表中,然后再次 你添加第一个复制的元素......它们已经在链表中。
A LinkedList
为您处理元素的插入(这就是使用集合的目的)。您不需要 shift 任何东西或复制您自己。
为了便于说明,在 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);
}