英文:
Unable to see ArrayList elements in another object in Java
问题
以下是翻译好的内容:
我正在尝试查看另一个类中ArrayList的元素,但它显示没有任何内容。在另一个类中,您可以看到ArrayList中的元素。
以下是代码。
第一个类
import java.util.ArrayList;
public class Test {
ArrayList<String> inventory = new ArrayList<String>();
public void test() {
inventory.add("item");
}
public void check() {
System.out.println(inventory);
}
}
第二个类
import java.util.ArrayList;
public class Test2 {
Test testObj = new Test();
public void test2() {
System.out.println(testObj.inventory);
}
}
主类
public class Main {
public static void main(String[] args) {
Test testObj = new Test();
Test2 test2Obj = new Test2();
testObj.test();
testObj.check();
test2Obj.test2();
}
}
输出
[item]
[]
英文:
I'm trying to view the elements of an ArrayList in a different class but it shows that there is nothing. In the other class, you can see the element in the ArrayList though.
Here are the codes.
First class
import java.util.ArrayList;
public class Test {
ArrayList<String> inventory = new ArrayList<String>();
public void test() {
inventory.add("item");
}
public void check() {
System.out.println(inventory);
}
}
Second class
import java.util.ArrayList;
public class Test2 {
Test testObj = new Test();
public void test2() {
System.out.println(testObj.inventory);
}
}
Main class
public class Main {
public static void main(String[] args) {
Test testObj = new Test();
Test2 test2Obj = new Test2();
testObj.test();
testObj.check();
test2Obj.test2();
}
}
Output
[item]
[]
答案1
得分: 0
在你的主方法中,你调用了处理添加操作的方法,但在第二个类中却没有调用任何添加方法,在你的主方法中也没有调用。
testObj.test();
testObj.check();
这个示例展示了你向数组列表中添加了一个项并将其显示出来。但在第二个类中,你没有添加任何东西,只是调用了它。因此,无法从该方法中显示任何元素。需要做的是在第二个类内部调用添加方法,然后尝试使用显示方法。
英文:
In your main method, you call the method that handles adding operation yet you do not call any add methods in the second class and in your main method.
testObj.test();
testObj.check();
This one shows that you add an item into the arraylist and show it. But in the second class you do not add anything but calling it. Therefore there is no way to show any element from that method. What has should be done is that you just need to call the adding method inside of the second class and then try to use the display method.
答案2
得分: 0
在Java中,类级别的变量在未被声明为static
的情况下与对象相关联。
所谓与对象相关联,是指当在Java中使用new
关键字初始化对象时,所有对象都将拥有自己的内存和自己的类变量。
在您的情况下,在main
方法内部,当您创建Test
类的对象,并在Test2
类内部创建另一个测试类对象时,两者将彼此完全独立。它们都拥有自己的内存和变量。
如果您希望从第一个对象的inventory
变量中获取更新,并在另一个对象中可用,您需要将其声明为static
并在静态块内进行初始化。静态变量在类的所有对象之间共享。如下所示:
static ArrayList<String> inventory;
static {
inventory = new ArrayList<String>();
}
英文:
In java Class level variables are Object associated until unless they are not declared static
.
By means of Object associated is that when a object is initialized with new keyword in Java all Object will be having it's own memory and own class variables.
In your case inside main method when you create a Object of Test
class and when you create a another test class object inside Test2 class, both will be totally independent of each other. And both will be having their own memory and own variables.
If you want that to get the updates of inventory
variables from first object to be available in another object you need to declare it as static and should be initialized inside static block. Static variables are shared across all objects of class. As below:
static ArrayList<String> inventory;
static {
inventory = new ArrayList<String>();
}
答案3
得分: 0
你没有在Test2
类中调用test()
方法。
为了使它正常工作,请将Test2
类更改如下。
import java.util.ArrayList;
public class Test2 {
Test testObj = new Test();
public void test2() {
testObj.test();
System.out.println(testObj.inventory);
}
}
英文:
You didn't call test()
method in Test2
class.
To make it work change the Test2
class as follows.
import java.util.ArrayList;
public class Test2 {
Test testObj = new Test();
public void test2() {
testObj.test();
System.out.println(testObj.inventory);
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论