Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to access an ArrayList of another class?

Tags:

java

arraylist

Hello I'm a beginner in Java and this is my question: I have this first class with the following variables:

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?

like image 972
Imonar Smith Avatar asked May 09 '13 12:05

Imonar Smith


4 Answers

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

like image 155
Konstantin Yovkov Avatar answered Oct 21 '22 12:10

Konstantin Yovkov


You can do this by providing in class 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.

By the way, there is a strong convention that Java class names are uppercased.

Case 1 (simple getter):

public class Numbers {
      private List<Integer> list;
      public List<Integer> getList() { return list; }
      ...
}

Case 2 (non-modifiable wrapper):

public class Numbers {
      private List<Integer> list;
      public List<Integer> getList() { return Collections.unmodifiableList( list ); }
      ...
}

Case 3 (specific methods):

public class Numbers {
      private List<Integer> list;
      public void addToList( int i ) { list.add(i); }
      public int getValueAtIndex( int index ) { return list.get( index ); }
      ...
}
like image 28
Andy Thomas Avatar answered Oct 21 '22 13:10

Andy Thomas


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);
    }
  }
like image 3
Supermalf Avatar answered Oct 21 '22 13:10

Supermalf


Two ways

1)instantiate the first class and getter for arrayList

or

2)Make arraylist as static

And finally

Java Basics By Oracle

like image 1
Suresh Atta Avatar answered Oct 21 '22 12:10

Suresh Atta