Forums
New posts
Search forums
News
Security News
Technology News
Giveaways
Giveaways, Promotions and Contests
Discounts & Deals
Reviews
Users Reviews
Video Reviews
Support
Windows Malware Removal Help & Support
Inactive Support Threads
Mac Malware Removal Help & Support
Mobile Malware Removal Help & Support
Blog
Log in
Register
What's new
Search
Search titles only
By:
Search titles only
By:
Reply to thread
Menu
Install the app
Install
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Guides
Programming Guides & Questions
ArrayList in Java (simple examples and quick tutorial)
Message
<blockquote data-quote="JM Safe" data-source="post: 783809" data-attributes="member: 35684"><p>Hello guys,</p><p></p><p>as I said I will talk about ArrayList in Java. ArrayList are a more sophisticated structure than arrays, but they are a bit different than the arrays. When you initialize an ArrayList variable you don't have to specify the length of the ArrayList, it is a flexible data structure. Here is some examples of using ArrayList:</p><p></p><p>[CODE=java]ArrayList<String> exampleArrayList =new ArrayList<String>(); //there we create our arraylist, and the type is "String", so our arraylist will contain only "String" elements.</p><p>exampleArrayList.add("M"); //there we add the element to our arraylist</p><p>exampleArrayList.add("T"); //there we add the second element of our arraylist</p><p></p><p>//to remove an element is pretty similar and easy:</p><p></p><p>exampleArrayList.remove("M");</p><p></p><p>//use this if you want to remove ALL elements from the arraylist:</p><p></p><p>exampleArrayList.clear();</p><p></p><p>//it is easy also to get an element present at a certain position in the list:</p><p></p><p>String firstElement = exampleArrayList.get(0); //there we get the first element of the list (index 0)</p><p></p><p>//here is instead a method to verify an arraylist is empty (it doesn't contain elements) or not:</p><p></p><p>boolean empty = exampleArrayList.isEmpty(); //we save the output of this method in a boolean variable (true or false)</p><p></p><p>//if we want to know if a certain element is contained in an arraylist we can write that:</p><p></p><p>if(exampleArrayList.contains("M"))</p><p>{</p><p> System.out.println("yes, it contains M");</p><p>}</p><p></p><p>//there, instead, we will loop through our ArrayList:</p><p></p><p>Iterator iteratorList = exampleArrayList.iterator(); </p><p></p><p>while(iteratorList.hasNext()) { //we verify if we reached the end of our ArrayList or there are others elements to write</p><p></p><p>System.out.println(iteratorList.next()); //let's write ArrayList elements</p><p></p><p>} </p><p></p><p></p><p>//if you want to get the index (position) of a certain element contained in the arraylist you can do that:</p><p></p><p>int positionOfElement = exampleArrayList.indexOf("M"); //it should return 0 because "M" is the first element of the arraylist.</p><p></p><p>//the last method I want to describe in this thread is "size()", which let us to know the size of our arraylist:</p><p></p><p>int lengthList = exampleArrayList.size();</p><p></p><p>[/CODE]</p><p></p><p>ArrayList structures are faster (in terms of execution and performance) than arrays. So... what is the best? Arrays or ArrayLists? It depends on the project you want to develop; surely arrays can be useful if you want a fixed length of your list, but if you need for example a fast, and dynamic, flexible list I would go for ArrayLists.</p><p>If you want to know more about ArrayLists in Java and discover other useful and powerful methods you can click here: <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" target="_blank">ArrayList (Java Platform SE 8 )</a></p><p></p><p>Thank you all! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite110" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" /></p></blockquote><p></p>
[QUOTE="JM Safe, post: 783809, member: 35684"] Hello guys, as I said I will talk about ArrayList in Java. ArrayList are a more sophisticated structure than arrays, but they are a bit different than the arrays. When you initialize an ArrayList variable you don't have to specify the length of the ArrayList, it is a flexible data structure. Here is some examples of using ArrayList: [CODE=java]ArrayList<String> exampleArrayList =new ArrayList<String>(); //there we create our arraylist, and the type is "String", so our arraylist will contain only "String" elements. exampleArrayList.add("M"); //there we add the element to our arraylist exampleArrayList.add("T"); //there we add the second element of our arraylist //to remove an element is pretty similar and easy: exampleArrayList.remove("M"); //use this if you want to remove ALL elements from the arraylist: exampleArrayList.clear(); //it is easy also to get an element present at a certain position in the list: String firstElement = exampleArrayList.get(0); //there we get the first element of the list (index 0) //here is instead a method to verify an arraylist is empty (it doesn't contain elements) or not: boolean empty = exampleArrayList.isEmpty(); //we save the output of this method in a boolean variable (true or false) //if we want to know if a certain element is contained in an arraylist we can write that: if(exampleArrayList.contains("M")) { System.out.println("yes, it contains M"); } //there, instead, we will loop through our ArrayList: Iterator iteratorList = exampleArrayList.iterator(); while(iteratorList.hasNext()) { //we verify if we reached the end of our ArrayList or there are others elements to write System.out.println(iteratorList.next()); //let's write ArrayList elements } //if you want to get the index (position) of a certain element contained in the arraylist you can do that: int positionOfElement = exampleArrayList.indexOf("M"); //it should return 0 because "M" is the first element of the arraylist. //the last method I want to describe in this thread is "size()", which let us to know the size of our arraylist: int lengthList = exampleArrayList.size(); [/CODE] ArrayList structures are faster (in terms of execution and performance) than arrays. So... what is the best? Arrays or ArrayLists? It depends on the project you want to develop; surely arrays can be useful if you want a fixed length of your list, but if you need for example a fast, and dynamic, flexible list I would go for ArrayLists. If you want to know more about ArrayLists in Java and discover other useful and powerful methods you can click here: [URL='https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html']ArrayList (Java Platform SE 8 )[/URL] Thank you all! ;) [/QUOTE]
Insert quotes…
Verification
Post reply
Top