@Data
public class Node<E> {
private E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this.e = e;
this.next = null;
}
public Node(E[] input){
if(input==null){
return;
}
this.setE(input[0]);
if(input.length!=1){
Node cur=new Node(subInput(1,input.length,input));
this.setNext(cur);
}
}
private E[] subInput(int a, int length,E[] input) {
E[] newInput=(E[]) new Object[length-1];
for(int i=0;i<length-1;i++){
newInput[i]= input[i+a];
}
return newInput;
}
public Node() {
this(null, null);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
Node cur = this;
while (cur != null) {
stringBuilder.append(cur.getE() + "->");
cur = cur.getNext();
}
stringBuilder.append("NULL");
return stringBuilder.toString();
}
public static void main(String[] args){
Node<Integer> head=new Node(new int[]{1,2,3,4,5});
System.out.println(head.toString());
}
}
我在main方法里new Node(new int[]{1,2,3,4,5}) 以为调用了Node(E[] input)的构造参数,但是实质上指向的是Node(E e)
如果
public static void main(String[] args){
Node<Integer> head=new Node(new Integer[]{1,2,3,4,5});
System.out.println(head.toString());
}
就是调用了Node(E[] input)的构造参数,基本数据类型的构成的数组,系统默认为一个对象,对象数据类型Integer才被识别为一个数组