Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Java
You last visited: Today at 21:16

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



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

Discussion on Anyone able to explain me arrays in java? Usage, why, how, when etc. within the Java forum part of the Coders Den category.

Reply
 
Old   #1


 
Omni's Avatar
 
elite*gold: 52
The Black Market: 122/0/0
Join Date: Apr 2015
Posts: 1,928
Received Thanks: 122
Anyone able to explain me arrays in java? Usage, why, how, when etc.

Topic. I know how to make an array, but how to use it later or express the values inside of it?..
Omni is offline  
Old 09/26/2016, 14:05   #2



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
Shawak is offline  
Thanks
1 User
Old 09/27/2016, 10:15   #3
dotCom
 
Devsome's Avatar
 
elite*gold: 9842
The Black Market: 107/0/0
Join Date: Mar 2009
Posts: 16,843
Received Thanks: 4,675
#moved
Devsome is offline  
Old 10/01/2016, 09:40   #4
 
XYZ-Cannon's Avatar
 
elite*gold: 0
Join Date: Jul 2011
Posts: 1,773
Received Thanks: 866
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.
XYZ-Cannon is offline  
Thanks
1 User
Old 10/04/2016, 18:37   #5


 
Menan's Avatar
 
elite*gold: 0
The Black Market: 169/0/0
Join Date: Sep 2008
Posts: 9,484
Received Thanks: 3,109
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.
Menan is offline  
Reply


Similar Threads Similar Threads
Java: Arrays auslesen, speichern und vergleichen?!
09/30/2011 - General Coding - 22 Replies
Hey E*pvpers. Ich besuche seid Anfang dieses Schuljahres jetzt den InfoLk. Wir programmieren und werden auch weiterhin vorerst, in Java programmieren und benutzen den Compiler BlueJ wobei das ja eher nebensächlich ist. Nun zu meinem Problem. Wir sind angefangen an einem Projekt "Schule" zu arbeiten.
[Java] Code-Beispiele für String, Arrays und und...
06/02/2010 - Coding Tutorials - 9 Replies
Joa, da ich Heute meine Informatik Prüfung in der Uni hatte ( und es sollte wohl min. eine 2 raus gekommen sein ) hatte ich Gestern so in ner Stunde einige Code Beispiele für verschiedene Dinge geschrieben. Eins war wie man Strings verarbeiten kann, was man mit Arrays anstellen kann und noch so paar Allgemeine Sachen zur Vererbung und Klassen. Man sollte das hier nicht unbedingt als Tutorial sehen da ich euch eigentlich hier einfach nur Code reinhau den ihr direkt Ausführen könnt wenn ihr...
Hilfe in Info mit Arrays via Java
12/03/2009 - General Coding - 9 Replies
Heho,ich sitz grad hilflos in Info. Kann mir wer mit dieser Aufgabenstellung helfen? Berechnen Sie die Fakultät bis 10! und speichern Sie sie in einem Array. (Unter 5! gesprochen "Fünf Fakultät", versteht man als 1*2*3*4*5=120. Es wird definiert: 0! = 1) wär nett, lg



All times are GMT +1. The time now is 21:19.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.