Create an empty linkedlist by using ListIterator. Insert several Integers into this List, when inserting, always insert into the middle of the List

The add() method in ListIterator can just achieve this function. Let's start to introduce the add() method (here I directly quote goodbaby728's blog, if there is any infringement, please delete it immediately after contacting me)


The final output is ADEBC

The solution code for this problem is as follows:

package p411;


import java.util.LinkedList;
import java.util.ListIterator;
public class MiddleInsert2 {
	static void Insert(LinkedList<Integer> linkedList,Integer [] ia){
		for(Integer i:ia){
			ListIterator<Integer> lt = linkedList.listIterator((linkedList.size()/2));//ListIterator points directly to each loop
																					//Middle of linkedList
			lt.add(i);
		}
		System.out.println(linkedList);
	}
	static class Test{
		public static void main(String[] args) {
			LinkedList<Integer>linkedList =new LinkedList<Integer>();
			Integer[] a =new Integer[]{1,2,3,4,5,6,7};
			Insert(linkedList,a);
		}
	}

}

Thoughts: I have wanted to use two ListIterators for this operation before, li2 operates on a given column of Integers, and li1 operates on the column to be output. The specific code is as follows:

package p411;
import java.util.LinkedList;
import java.util.ListIterator;


public class MiddleInsert {
	LinkedList<Integer> linkedList =new LinkedList<Integer>();
	ListIterator>Integer> li1 = linkedList.listIterator ();
	ListIterator<Integer> li2;
	MiddleInsert(Integer[] a){
		LinkedList<Integer> reserlinkList =new LinkedList<Integer>();
		// Pass all the data in the array a
		for(Integer i :a){
			reserlinkList.add(i);
//			linkedList.add(i);
		}
// li1 = linkedList.listIterator ();
		li2 = reserlinkList.listIterator ();
		for(int i=0;i<a.length;i++){
			if(i%2==0){
					li1.next();
					li1.set(li2.next());
				}
				else{
					li1.previous();//The problem is here
					li1.set(li2.next());
				}
			
		}
	}
	void out(){
		System.out.println(linkedList);
	}
	static class Test{
		public static void main(String[] args) {
			Integer[] a=new Integer[]{1,2,3,4,5};
			MiddleInsert mid =new MiddleInsert(a);
			mid.out();
		}
	}

}

Among them, li2 moves sequentially, li1 moves forward once, and moves backward once to complete the problem. Always insert in the middle.

But the result obtained in the operation is wrong. After careful thinking, I think that when the linkList is empty at first, when I insert a value, there is an element in it. When I want to insert it again, there is a problem. Call li1.previous( ) method. Because there is only one element in the list column, it can move forward if it does not exist.

Thoughts: The ListIterator.add() method is not mentioned in thinking in java, so I personally guess that there may be other solutions to this problem, and, for the ListIterator.add() method, it seems that there is only one element in the column. Inserts the element in front of the element. (I wonder if it is because .add() points to the position after .previous() and before the .next() method. After reading the top paragraph, I probably understand that this is the case). For Iterator and ListIterator, I personally think that ListIterator is much more powerful than Iterator. The latter provides more methods, and two more critical points are that it has more "write" functions and two-way movement functions in addition to "read".

In addition, in the process of writing code, I often have null pointer exceptions, that is, the List table is not created, and I just use ListIterator it =list1.ListIterator() after declaring the list. More attention should be paid to this in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650971&siteId=291194637