Java - 如何访问另一个类的 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16462163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to access an ArrayList of another class?
提问by Imonar Smith
Hello I'm a beginner in Java and this is my question: I have this first class with the following variables:
您好,我是 Java 的初学者,这是我的问题:我有一个带有以下变量的第一类:
import java.util.ArrayList;
public class numbers {
private int number1 = 50;
private int number2 = 100;
}
And I have this class too:
我也有这门课:
import java.util.ArrayList;
public class test {
private numbers number;
}
My question here is: I want to store the number1 & number2 variables into an ArrayList, then access this ArrayList from class test. How can I do that?
我的问题是:我想将 number1 和 number2 变量存储到一个 ArrayList 中,然后从类 test 访问这个 ArrayList。我怎样才能做到这一点?
采纳答案by Konstantin Yovkov
import java.util.ArrayList;
public class numbers {
private int number1 = 50;
private int number2 = 100;
private List<Integer> list;
public numbers() {
list = new ArrayList<Integer>();
list.add(number1);
list.add(number2);
}
public List<Integer> getList() {
return list;
}
}
And the test class:
和测试类:
import java.util.ArrayList;
public class test {
private numbers number;
//example
public test() {
number = new numbers();
List<Integer> list = number.getList();
//hurray !
}
}
回答by Suresh Atta
Two ways
两种方式
1)instantiate the first class
and getter for arrayList
1)实例化 first class
和吸气剂arrayList
or
或者
2)Make arraylist as static
2)将数组列表设为 static
And finally
最后
回答by Andy Thomas
You can do this by providing in class numbers
:
您可以通过在课堂上提供numbers
:
- A method that returns the ArrayList object itself.
- A method that returns a non-modifiable wrapper of the ArrayList. This prevents modification to the list without the knowledge of the class numbers.
- Methods that provide the set of operations you want to support from class numbers. This allows class numbers to control the set of operations supported.
- 返回 ArrayList 对象本身的方法。
- 返回 ArrayList 的不可修改包装器的方法。这可以防止在不知道类别编号的情况下修改列表。
- 提供要从类号支持的一组操作的方法。这允许类号控制支持的操作集。
By the way, there is a strong convention that Java class names are uppercased.
顺便说一下,Java 类名是大写的,这是一个强有力的约定。
Case 1 (simple getter):
案例 1(简单的 getter):
public class Numbers {
private List<Integer> list;
public List<Integer> getList() { return list; }
...
}
Case 2 (non-modifiable wrapper):
情况 2(不可修改的包装器):
public class Numbers {
private List<Integer> list;
public List<Integer> getList() { return Collections.unmodifiableList( list ); }
...
}
Case 3 (specific methods):
案例3(具体方法):
public class Numbers {
private List<Integer> list;
public void addToList( int i ) { list.add(i); }
public int getValueAtIndex( int index ) { return list.get( index ); }
...
}
回答by Malf
You can do the following:
您可以执行以下操作:
public class Numbers {
private int number1 = 50;
private int number2 = 100;
private List<Integer> list;
public Numbers() {
list = new ArrayList<Integer>();
list.add(number1);
list.add(number2);
}
int getNumber(int pos)
{
return list.get(pos);
}
}
public class Test {
private Numbers numbers;
public Test(){
numbers = new Numbers();
int number1 = numbers.getNumber(0);
int number2 = numbers.getNumber(1);
}
}
回答by Adrian Zaharia
Put them in an arrayList in your first class like:
将它们放在您的第一堂课的 arrayList 中,例如:
import java.util.ArrayList;
public class numbers {
private int number1 = 50;
private int number2 = 100;
public ArrayList<int> getNumberList() {
ArrayList<int> numbersList= new ArrayList<int>();
numbersList.add(number1);
numberList.add(number2);
....
return numberList;
}
}
Then, in your test class you can call numbers.getNumberList() to get your arrayList. In addition, you might want to create methods like addToList / removeFromList in your numbers class so you can handle it the way you need it.
然后,在您的测试类中,您可以调用 numbers.getNumberList() 来获取您的 arrayList。此外,您可能希望在 numbers 类中创建诸如 addToList / removeFromList 之类的方法,以便您可以按照需要的方式处理它。
You can also access a variable declared in one class from another simply like
您还可以从另一个类中访问在一个类中声明的变量,就像
numbers.numberList;
if you have it declared there as public.
如果您将其声明为公开。
But it isn't such a good practice in my opinion, since you probably need to modify this list in your code later. Note that you have to add your class to the import list.
但在我看来,这不是一个好的做法,因为您可能需要稍后在代码中修改此列表。请注意,您必须将您的类添加到导入列表中。
If you can tell me what your app requirements are, i'll be able tell you more precise what i think it's best to do.
如果您能告诉我您的应用程序要求是什么,我将能够更准确地告诉您我认为最好的做法。