使用toarray方法返回数组之后,输出是一个数组地址,在把地址赋给新实例化的数组对象又回报错
//链表
import org.w3c.dom.*;
class Link{
private class Node{
private Object Name;
private Node next;
public Node(Object Name){
this.Name = Name;
}
public Object getName(){
return this.Name;
}
public void addNode(Node node){
if(this.next == null){
this.next = node;
}else{
this.next.addNode(node);
}
}
public void printNode(){
System.out.println(this.Name);
if(this.next != null){
this.next.printNode();
}
}
public boolean Contains(String name){
if(this.Name == name){
return true;
}else if(this.next != null){
return this.next.Contains(name);
}else{
return false;
}
}
public Object getNode(int number){
if(number == Link.this.num ++){
return this.Name;
}else{
return this.next.getNode(number);
}
}
public boolean setname(int number,Object name){
if(Link.this.num ++ ==number){
this.Name = name;
}else{
if(this.next != null){
return this.next.setname(number,name);
}
}
return false;
}
public void Remove(Node previous,Object name){
if(this.Name == name){
previous.next = this.next;
}else{
this.next.Remove(this,name);
}
}
public void toArray(){
Link.this.array[Link.this.num ++] = this.Name;
if(this.next != null){
this.next.toArray();
}
}
}
private Node root;
private int count = 0;
private int num;
private Object [] array;
public void add(Object Name){
if(Name == null){
return;
}
Node newNode = new Node(Name);
if(this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
count ++;
}
public void print(){
if(this.root != null){
this.root.printNode();
}
}
public int size(){
return this.count;
}
public boolean contains(String name){
if(name == null || root == null){
return false;
}else{
return this.root.Contains(name);
}
}
public boolean isEmpty(){
if(root == null || count == 0){
return false;
}else{
return true;
}
}
public Object get(int number){
if(number > count){
return null;
}else {
this.num = 0;
return this.root.getNode(number);
}
}
public boolean set(int number,Object Name){
if(number > this.count){
return false;
}
this.num = 0;
return this.root.setname(number,Name);
}
public void remove(String name){
if(this.root.Contains(name)){
if(this.root.Name == name){
this.root = this.root.next;
}else{
this.root.next.Remove(this.root,name);
}
count --;
return ;
}
}
public Object toarray(){
if(this.root == null){
return null ;
}
num = 0;
this.array= new Object[this.count];
this.root.toArray();
return this.array;
}
}
public class Main
{
public static void main(String Args[]){
Link a = new Link();
a.add("hello");
a.add("world");
a.add("jwjwj");
Object obj = a.toarray();
for(int x = 0;x<a.size();x ++){
System.out.print(obj[x]);
}
}
}
代码如上,求giegie们指点一二