简体   繁体   English

Java - 如何访问另一个类的ArrayList?

[英]Java - How to access an ArrayList of another class?

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. 我的问题是:我想将number1和number2变量存储到ArrayList中,然后从类test中访问这个ArrayList。 How can I do that? 我怎样才能做到这一点?

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 !
   }
}

You can do this by providing in class numbers : 您可以通过提供课程numbers来完成此操作:

  • A method that returns the ArrayList object itself. 一种返回ArrayList对象本身的方法。
  • A method that returns a non-modifiable wrapper of the ArrayList. 一种返回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. 这允许类号控制支持的操作集。

By the way, there is a strong convention that Java class names are uppercased. 顺便说一句,有一个强烈的约定,Java类名称是大写的。

Case 1 (simple getter): 案例1(简单的吸气剂):

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 ); }
      ...
}

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);
    }
  }

Two ways 两种方式

1)instantiate the first class and getter for arrayList 1)实例化arrayListfirst class和getter

or 要么

2)Make arraylist as static 2)使arraylist成为static

And finally 最后

Java Basics By Oracle Java基础知识由Oracle提供

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. 然后,在您的测试类中,您可以调用numbers.getNumberList()来获取您的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. 此外,您可能希望在数字类中创建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. 如果您能告诉我您的应用程序要求是什么,我将能够更准确地告诉您我认为最好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM