ArrayList在另一个类中访问时为空。

huangapple go评论86阅读模式
英文:

ArrayList empty while accessing it in another class

问题

  1. 准备一个玩具类。
public class toy {

  public String name;
  public int id;
  public int price;

  public toy(String name, int id, int price) {
	  this.name = name;
	  this.id = id;
	  this.price = price;
  }
  public String getName() {
	  return name;
  }
  public void setName(String name) {
	  this.name = name;
  }
  public int getId() {
	  return id;
  }
  public void setId(int id) {
	  this.id = id;
  }
  public int getPrice() {
	  return price;
  }
  public void setPrice(int price) {
	  this.price = price;
  }
}
  1. 准备另一个类来保存一组玩具。同样的类提供了用于该玩具列表的 getter 和 setter 方法。
import java.util.ArrayList;
public class accMeth {

    public static ArrayList<toy> toylist=new ArrayList<toy>();

    public ArrayList<toy> getToylist() {
	    return toylist;
    }

    public void setToylist(ArrayList<toy> toylist) {
	   this.toylist = toylist;
    }

}
  1. 以下这个类创建并将玩具添加到 toyList 中。然后使用 setter 设置 toyList。
import java.util.ArrayList;
import java.util.List;

public class adding extends accMeth {
    public static void main(String[] args) {

        toy t1= new toy("gg",1,20);
        toy t2 = new toy("gg",2,23);

        accMeth meth=new accMeth();

        toylist.add(t1);
        toylist.add(t2);
        meth.setToylist(toylist);
        System.out.println(meth.getToylist().get(1).getPrice());
    }
}
  1. 现在如果我想访问列表,这里显示为空。
public class getting{

    public static void main(String[] args) {

        adding ad= new adding();
        System.out.println(ad.getToylist().isEmpty());
    }
}
英文:
  1. Prepared a toy class.

    public class toy {
    
      public String name;
      public int id;
      public int price;
    
      public toy(String name, int id, int price) {
    	  this.name = name;
    	  this.id = id;
    	  this.price = price;
      }
      public String getName() {
    	  return name;
      }
      public void setName(String name) {
    	  this.name = name;
      }
      public int getId() {
    	  return id;
      }
      public void setId(int id) {
    	  this.id = id;
      }
      public int getPrice() {
    	  return price;
      }
      public void setPrice(int price) {
    	  this.price = price;
      }
    }
    
  2. Prepared another class to hold as list of toys. Same class provides getter and setter methods for the list of toys.

    import java.util.ArrayList;
    public class accMeth {
    
        public static ArrayList&lt;toy&gt; toylist=new ArrayList&lt;toy&gt;();
    
        public ArrayList&lt;toy&gt; getToylist() {
            return toylist;
        }
    
        public void setToylist(ArrayList&lt;toy&gt; toylist) {
           this.toylist = toylist;
        }
    
    }
    
  3. This class below creates and adds toys in toyList. Then setting the toyList using setter.

     import java.util.ArrayList;
     import java.util.List;
    
     public class adding extends accMeth {
         public static void main(String[] args) {
    
     	    toy t1= new toy(&quot;gg&quot;,1,20);
     	    toy t2 = new toy(&quot;gg&quot;,2,23);
    
     	    accMeth meth=new accMeth();
    
     		toylist.add(t1);
     		toylist.add(t2);
         	meth.setToylist(toylist);
             System.out.println(meth.getToylist().get(1).getPrice());
         }
     }
    
  4. Now if I want to access list, here it appears to be empty.

     public class getting{
    
         public static void main(String[] args) {
    
         	adding ad= new adding();
     	    System.out.println(ad.getToylist().isEmpty());
         }
     }
    

答案1

得分: 1

这是您要翻译的内容:

它是空的,因为你在第3个实例中填充它,然后从第4个实例中进行打印。

主要的要点是 `public void setToylist(ArrayList<toy> toylist) {
    this.toylist = toylist;
}`,你使用了 `this`,它会将值添加到这个实例中。

如果你想要在第2个地方使用静态 `defend`,则从设置方法中删除 `this` 并以静态方式访问它。

    public static void setToylist(ArrayList<toy> toylistar) {
    toylist = toylistar;
    }

现在你将这个方法标记为静态,所以现在你必须静态地使用它。

    toy t1 = new toy("gg", 1, 20);
    toy t2 = new toy("gg", 2, 23);
    
    

     toylist.add(t1);
     toylist.add(t2);
    accMeth.setToylist(toylist);
    
    System.out.println(accMeth.getToylist().get(1).getPrice());
    

    }

现在你可以在主类中找到变化。

    public static void main(String[] args) {
    
    adding ad = new adding();
    System.out.println(accMeth.getToylist().isEmpty());
    

    }

但不要忘记在第3个地方的构造函数中加上添加的部分。



    import java.util.ArrayList;
    import java.util.List;

    public class adding extends accMeth {
        public adding() {

            toy t1 = new toy("gg", 1, 20);
            toy t2 = new toy("gg", 2, 23);
    

            toylist.add(t1);
            toylist.add(t2);
            accMeth.setToylist(toylist);
    
            System.out.println(accMeth.getToylist().get(1).getPrice());

        }
    }
英文:

it's empty because you fill it in an instance in 3 and printing from a new instance in 4

the main point is that public void setToylist(ArrayList&lt;toy&gt; toylist) {
this.toylist = toylist;
}
you used this and it will add the value to this instant

if you want to use the static defend in 2 delete the this from the set method and access it as Static

public static void setToylist(ArrayList&lt;toy&gt; toylistar) {
toylist = toylistar;
}

now you marked the method as static and now you have to use it statickly

toy t1= new toy(&quot;gg&quot;,1,20);
toy t2 = new toy(&quot;gg&quot;,2,23);



 toylist.add(t1);
 toylist.add(t2);
accMeth .setToylist(toylist);

System.out.println(accMeth.getToylist().get(1).getPrice());


}

now you can find the change in the main

public static void main(String[] args) {

adding ad= new adding();
System.out.println(accMeth.getToylist().isEmpty());


}

but don't forget to put the add to contracter in number 3

import java.util.ArrayList; import java.util.List;

public class adding extends accMeth { public  adding () {

toy t1= new toy(&quot;gg&quot;,1,20);
toy t2 = new toy(&quot;gg&quot;,2,23);


 toylist.add(t1);
 toylist.add(t2);
accMeth.setToylist(toylist);

System.out.println(accMeth.getToylist().get(1).getPrice());


 }

答案2

得分: 1

1. Toy.java

> nameid 和 price 变量应该是私有的构造函数应该是 Toy 而不是 toy

    public class Toy {

        private String name;
        private int id;
        private int price;

        public Toy(String name, int id, int price) {
            this.name = name;
            this.id = id;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }
    }

2. AccMeth 类

> 应该是 AccMeth。'toylist' 不应该是 publicstatic因为你已经使用了 getter 和 setter而且你会修改它查看 [Java 访问修饰符][1]

    public class AccMeth {

        private ArrayList<Toy> toylist = new ArrayList<>();

        public ArrayList<Toy> getToylist() {
            return toylist;
        }

        public void setToylist(ArrayList<Toy> toylist) {
            this.toylist = toylist;
        }
    }

3. Adding 类

> 不需要扩展其他类因为在你的代码中已经存在我的意思是你可以直接从执行点所在的类即包含 main 方法的类访问 ArrayList<Toy>

    public class Adding {

        public static void main(String[] args) {
            Toy t1 = new Toy("gg", 1, 20);
            Toy t2 = new Toy("gg", 2, 23);

            AccMeth meth = new AccMeth();
            ArrayList<Toy> toylist = meth.getToylist();
            toylist.add(t1);
            toylist.add(t2);
            meth.setToylist(toylist);

            System.out.println(meth.getToylist().get(1).getPrice());
        }
    }

  [1]: https://www.javatpoint.com/access-modifiers
英文:
  1. Class Toy.java:

> name, id and price variables should be private. Constructor should be
> Toy instead of toy

public class Toy {
private String name;
private int id;
private int price;
public Toy (String name, int id, int price) {
this.name = name;
this.id = id;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
  1. Class accMeth:

> Should be AccMeth. 'toylist' should not be public and static since,
> you have employed getter and setters. Plus, you will be modifying it. Check java Aceess Modifiers

public class AccMeth {
private ArrayList toylist = new ArrayList();
public ArrayList&lt;Toy&gt; getToylist() {
return toylist;
}
public void setToylist(ArrayList&lt;Toy&gt; toylist) {
this.toylist = toylist;
}
}
  1. class adding

> No need to extend the other class as it is in your code. I mean, you
> could directly access the ArrayList<Toy> from the class that is the
> execution point. i.e. holds the main method.

public class Adding {
public static void main(String[] args) {
Toy t1 = new Toy(&quot;gg&quot;, 1, 20);
Toy t2 = new Toy(&quot;gg&quot;, 2, 23);
AccMeth meth = new AccMeth();
ArrayList&lt;Toy&gt; toylist = meth.getToylist();
toylist.add(t1);
toylist.add(t2);
meth.setToylist(toylist);
System.out.println(meth.getToylist().get(1).getPrice());
}
}

答案3

得分: -1

你应该将代码块:"将玩具添加到列表" 移动到构造函数中。

英文:

you should move block code: "adding toy to list" to adding constructor

huangapple
  • 本文由 发表于 2020年10月13日 17:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64332696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定