英文:
ArrayList empty while accessing it in another 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;
}
}
- 准备另一个类来保存一组玩具。同样的类提供了用于该玩具列表的 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;
}
}
- 以下这个类创建并将玩具添加到 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());
}
}
- 现在如果我想访问列表,这里显示为空。
public class getting{
public static void main(String[] args) {
adding ad= new adding();
System.out.println(ad.getToylist().isEmpty());
}
}
英文:
-
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; } }
-
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<toy> toylist=new ArrayList<toy>(); public ArrayList<toy> getToylist() { return toylist; } public void setToylist(ArrayList<toy> toylist) { this.toylist = toylist; } }
-
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("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()); } }
-
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<toy> toylist) {
you used
this.toylist = toylist;
}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<toy> toylistar) {
toylist = toylistar;
}
now you marked the method as static and now you have to use it statickly
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());
}
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("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());
}
答案2
得分: 1
1. Toy.java 类:
> name、id 和 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' 不应该是 public 和 static,因为你已经使用了 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
英文:
- 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;
}
}
- 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<Toy> getToylist() {
return toylist;
}
public void setToylist(ArrayList<Toy> toylist) {
this.toylist = toylist;
}
}
- 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("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());
}
}
答案3
得分: -1
你应该将代码块:"将玩具添加到列表" 移动到构造函数中。
英文:
you should move block code: "adding toy to list" to adding constructor
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论