m0_68034795 2022-06-14 17:31 采纳率: 100%
浏览 51
已结题

java中返回数组之后怎么输出数组?

使用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们指点一二

  • 写回答

1条回答 默认 最新

  • 摆烂熊猫 2022-06-14 17:46
    关注

    public static void main(String Args[]){
    Link a = new Link();
    a.add("hello");
    a.add("world");
    a.add("jwjwj");
    Object[] obj = (Object[]) a.toarray();
    for(int x = 0;x<a.size();x ++){
    System.out.print(obj[x]);
    }
    }

    a.toarray() 返回的是一个Object,实际上你需要的是一个Object数组类型,需要强转,并且用Object[] 来接收

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 创建了问题 6月14日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)