Anyone able to explain me arrays in java? Usage, why, how, when etc.

09/26/2016 10:00 Omni#1
Topic. I know how to make an array, but how to use it later or express the values inside of it?..
09/26/2016 14:05 Shawak#2
[Only registered and activated users can see links. Click Here To Register...]
09/27/2016 10:15 Devsome#3
#moved
10/01/2016 09:40 XYZ-Cannon#4
Use ArrayLists instead of Arrays.

You use Arrays/ArrayLists to save data from the same type.

For example you got a class "Building". This class needs to save multiple instances from the type "Room".

You create an ArrayList:
Code:
ArrayList<Room> list_rooms = new ArrayList<Room>();
Now you can save multiple instances from the class "Room" into your ArrayList.
For example:
Code:
list_rooms.add(room);
list_rooms.add(room2);
Now you can access multiple rooms over one list with the command "get" (The class room has the function getName() in this example)
Code:
System.out.println(list_rooms.get(0).getName());
(0 means the first object of the list)

would give you some kind of this output
Code:
"Living room"

I hope this example shows you how to use them and why. You are able to keep similar data together in one variable and make them pretty easy to access.
10/04/2016 18:37 Menan#5
To try to explain you the difference between an array and an arraylist in java.

An array ist initialized with a defined amount of items it can hold (maximum).

Code:
int[] intArray = new int[5];
would be an array which could hold ints, but maximum 5 of them (position 0 - 4 in array).

An Arraylist is a java class of the standard jdk from oracle. Its oracles implementation of something you can use like you would use an array in another programming language. But the ArrayList is sometimes slower then an actual array (meassuring that time is part of MicroPerfomance-Tests).
So sometimes for perfomance reasons you want to use an "real" array.
You can use an array in java for example when you know the size of the array will never be bigger then a specified amount.


When you're a beginner stick to the ArrayList from java. It will do the things you want in the most easy way.

And maybe take a look at Google Guava later. It reimplements some of the Java Librarys in a so much better way. You can easily define arrays and use them (a must have when you write testclass for example or if you want to return a list from a stream of objects in a function).

Edit:

Array

- you have to know the maximum size of objects at construction
- Its more like you create many variables you can set imagine:
you want to save exaktly three Integers in your class.
Therefore you created this:
Code:
private int firstInt;
private int secondInt;
private int thirdInt;
But you could rather also just use:
Code:
private int[] intArray = new int[3];
and you would assign the different arrays.
Its more like you have 3 variables in one.
you would assign them like:
Code:
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
The ArrayList is a more complex implementation which manages an array for you.
Its an Instance of a class you create and use (see post above). Oracle implemented it for you.
Thats why you can call methods like add() or get() on that object.