20

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?

ollo
  • 24,797
  • 14
  • 106
  • 155
Imonar Smith
  • 379
  • 1
  • 5
  • 9
  • 2
    @alex23 - I disagree. It addresses a specific programming problem, which is perfectly fine for SO. Basic? Yes. But I don't see anything wrong with it. – Chris Forrence May 09 '13 at 12:53
  • 1
    (**1**) Declare and initialize your `ArrayList`. Use `add` method to place your `number1` and `number2` there. (**2**) Create a `public Getter` method for this ArrayList. (**3**) use `Getter` method to retrieve your ArrayList. – PM 77-1 May 09 '13 at 12:58
  • @alex23 Working code? I'm actually working on a project with a group members of my class, and the project is worth Zero points, and it has nothing to do with my computer sciences class, it's something we want to do, and if it was a part of a project we wouldn't go for an online help, our professor isn't stupid and he can figure it out. Plus, very little information is available on this particular problem online. Good thing not all people are like you, thanks for everyone else we got it. – Imonar Smith May 09 '13 at 13:05

5 Answers5

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

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Usually it's preferable to refer to an ArrayList object through the List interface. – Andy Thomas May 09 '13 at 13:03
  • You don't cast objects to different implementations. Using List as the type of the field and return value allows you to change the implementation without changing the code that uses it. It's better to use the interface as the default, unless there's a specific requirement to use the concrete class, rather than the other way around. – Andy Thomas May 09 '13 at 13:13
  • How can I get the size of this ArrayList ? list.size() doesn't work – Christos Michael Oct 05 '18 at 10:28
4

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 ); }
      ...
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • In the 3rd case it would be useful to tell others about size of the list, so that they don't attempt to access elements beyond its size, maybe a `getSize` method? Also instead of unmodifiable list you could simply return its copy. – user5539357 Mar 20 '16 at 19:42
  • @user5539357 - Yes, in case 3, `size()` would be useful. However, in case 2, returning an unmodifiable view of the list is O(1), while copying would be O(n). The implementation above leaves the choice and cost of copying up to the caller. – Andy Thomas Mar 20 '16 at 22:14
  • This answer is 3rd best scoring but better and older than the top two. Better because it's well explained and laid out. – pateksan Jan 04 '20 at 00:29
3

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);
    }
  }
Supermalf
  • 346
  • 4
  • 16
1

Two ways

1)instantiate the first class and getter for arrayList

or

2)Make arraylist as static

And finally

Java Basics By Oracle

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Put them in an arrayList in your first class like:

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.

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.

smt
  • 267
  • 6
  • 22
Adrian Zaharia
  • 115
  • 1
  • 1
  • 7