Reputation: 13
Conceptually what is going on here?
I have an ArrayList
ArrayList<SomeClass> myArray = new ArrayList<SomeClass>();
then I add something to it like this
myArray.add(new myArray(1, 2, 3));
Upvotes: 1
Views: 493
Reputation: 1853
You don't actually have an array, you have an ArrayList. An ArrayList is a resizable implementation of the List interface. This means, among other things, that you don't have to define a length like you would with an array.
I don't think the code you have provided will actually compile because when you go:
myArray.add(new myArray(1, 2, 3));
you haven't provided a type. You should go something like:
myArray.add(new ArrayList<Integer>(Arrays.asList(1,2,3)));
Using asList() will give almost the equivalent of assigning "default" values to an array. The array equivalent would look something like this:
Integer[] intArray = new Integer[] {1,2,3};
You should also parameterize your ArrayList
(s), this basically means you are saying what that ArrayList can hold. For instance:
ArrayList<Integer>
Can hold Integers.
The reason you must use a wrapper class (Integer
& not int
) is because Collections in Java don't permit you to store primitive types such as int
or double
.
You can have a Collection holding another Collection. In this case you could go:
ArrayList<ArrayList<Integer>> myArrayList = new ArrayList<ArrayList<Integer>>();
myArrayList.add(new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
myArrayList.add(new ArrayList<Integer>(Arrays.asList(9, 12, 24)));
for (ArrayList<Integer> index : myArrayList) {
System.out.println(index);
}
Here you have an ArrayList holding two other ArrayLists. This results in the printing of:
[1, 2, 3]
[9, 12, 24]
Upvotes: 0
Reputation: 85789
I have an array
ArrayList myArray = new ArrayList();
No, you don't have an array. Arrays are declared using brackes []
, like this:
int[] myArray;
You have an object of ArrayList
class. This object will use an array internally to store the data and dynamically change the array for a newer one when needs to assign more values.
then I add something to it like this
myArray.add(new myArray(1, 2, 3));
Conceptually, this is wrong and won't even compile. If you want to add more than one value in your array, you should use ArrayList#addAll
method, that receives another collection. Then, you have two ways to pass a Collection
in one statement:
Create a list using Arrays#asList
:
myArray.addAll(Arrays.asList(1, 2, 3));
You can use double brace initialization:
myArray.addAll(new ArrayList() {{
add(1);
add(2);
add(3);
}});
You should take into account that you should specify the generic for your ArrayList
instead of using raw data:
ArrayList<Integer> myArray = new ArrayList<Integer>();
myArray.addAll(new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
}});
More info:
Note that if you have an ArrayList
that is tied to a custom class, let's say ArrayList<SomeClass> myArrayList
, then the compiler prevents you to add elements to the list that doesn't pass the IS-A test (noted by instanceof
operator), so this code:
ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArrayList..addAll(new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
}});
Won't be able to compile since Integer
is not a SomeClass
. Similar with this piece of code:
ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArray.add(Arrays.asList(1, 2, 3));
But due to type erasure you can trick the compiler by using raw types. For example:
public void addData(ArrayList yourArrayList, Object anyData) {
yourArrayList.add(anyData);
}
Then in your code, you call this method:
ArrayList<String> myArrayList = new ArrayList<String>(); //allows Strings only
myArrayList.add("Hello world");
//myArrayList.add(0); //this won't compile because trying to add an Integer
addData(myArrayList, 0); //this will compile for using raw ArrayList and sending an Integer
System.out.println(myArrayList);
It will print:
[Hello world, 0]
Upvotes: 3
Reputation: 627
I think I know what you're trying to do, say you have this.
ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();
and you wanted to add an array to it, you're basically going to call
myArray.add(((Integer[]) new int[] { 0, 1, 2 }));
Which will add the array [0, 1, 2] to the next index in the ArrayList.
For exmaple, the following code
ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();
myArray.add(((Integer[]) new int[] { 0, 1, 2 }));
myArray.add(((Integer[]) new int[] { 1, 0, 2 }));
myArray.add(((Integer[]) new int[] { 3, 2, 1 }));
Would create the following results:
myArray index 0 = [0, 1, 2]
myArray index 1 = [1, 0, 2]
myArray index 2 = [3, 2, 1]
Is this your question?
Upvotes: 0
Reputation: 14413
myArray.add(new myArray(1, 2, 3));
This is not correct and don't compile, cause myArray
is not a type, it's just a reference, you need something of type integer or if you want to add another list, then use addAll
.
You could use something like this.
List<Integer> myArray = new ArrayList<>(); // specify with generics type parameter
//do some things and then
myArray.addAll(Arrays.asList(1,2,3)); // use this helper method to add a list to another list
Or if you want to only create and populate it, just use
List<Integer> myArray = Arrays.asList(1,2,3,4,5);
Upvotes: 0