Hey guys, today I thought I could show you guys what you can do with ArrayList, java's own build in Array+List :>, the ArrayList is in general slower than normal Arrays if used in larger codes and scales, however the usage of ArrayList comes in hand when you need to have a re-size able Array with implemented functions from a List.
So lets begin !
We start of by importing our packages and declare our class and main !
Now I chose to import the whole library than just one of the book(just aCode:import static javax.swing.JOptionPane.*; import javax.swing.*; import java.util.*; public class ArrayListTest { public static void main(String [] arg) {
metaphor)
To get only what we want to use, the import would look like this !
A small note !, when you import and close an import with ".*;" equals youCode:import java.util.ArrayList;
import the whole library.
Our ArrayList.
By using "ArrayList<String>" will every new Array be positioned and have a definition as a string ! So hence we will be working it strings, you could do Byte/Integer/Float/Double but not Char, due to not build in within ArrayList options !Code:ArrayList<String> AL = new ArrayList<String>();
So now let's go on with our options.
This will force our user to make a choice :>Code:while(true){ String Choice= showInputDialog(null,"1.Add an element in the list" +"\n2.Delete an element out of the list" +"\n3.Show all elements in the list" +"\n4.Delete all elements out of the list" +"\n5.Sort" +"\n6.Add an element in a free position" +"\n7 Close"); if(Choice== null) break;
On with the first option !
Here we will automatically add an element in the first position, so what did we do exactly?Code:else if(Choice.equals("1")){ String newElement = showInputDialog(null,"Add an element"); if(newElement == null) continue; AL.add(newElement); JOptionPane.showMessageDialog(null, "The element is now added !"); }
Here we worked in by adding our element; but will this really work?Code:AL.add(newElement);
Indeed it will, due to it's a re-size able Array. Which will give the ability to add our element in the first position(0) and etc. However if you want to deiced what position it shall take, then you will have to do it like this.
How it is build when you want to deiced.
How it looks when we implement it inCode:AL.add(Position,your element);
So let's go on with our second option !Code:AL.add(0,newElement);
Here is kind of a lot codes so let's make it simple and understandable !Code:else if(Choice.equals("2")){ AL.size(); if(AL.size()<1) { showMessageDialog(null,"You can't delete any elements there are non !"); continue; } String Del = showInputDialog("In what position? (0-"+(AL.size()-1)+")"); if(Del== null) continue; while(true){ try{ int Number= Integer.parseInt(Del); if(Number<0 || Number>= AL.size()) throw new Exception(); AL.remove(Number); break; } catch(Exception e){ Del = showInputDialog("You must chose between 0 and"+(AL.size()-1)+ "\nIn what position(0-"+(AL.size()-1)+")"); } } showMessageDialog(null, "Element is now deleted !"); }
We check our List if it contains anything, since we can't delete something with nothing in?
And that part is....
Next thing to explain, this partCode:if(AL.size()<1) { showMessageDialog(null,"You can't delete any elements there are non !");
Represents our real option since we want to delete "elements" when we have some in our list. So first by checking if it's there in our specific position.If nothing is typed in it will just continue and we will not delete anything !Code:String Del = showInputDialog("In what position? (0-"+(AL.size()-1)+")"); if(Del== null) continue; while(true){
However we want to delete so let's check it !
By casting a try/catch we will be able to have a decent app that does not crash in the middle of it's work !Code:try{ int Number= Integer.parseInt(Del); if(Number<0 || Number>= AL.size()) throw new Exception(); AL.remove(Number); break; } catch(Exception e){ Del = showInputDialog("You must chose between 0 and"+(AL.size()-1)+ "\nIn what position(0-"+(AL.size()-1)+")"); } } showMessageDialog(null, "Element is now deleted !");
So by checking if the input was greater than 0 but still not less than AL's size then it shall throw all exceptions!
So that's it!
Third option !
So this part is our "eyes" to check if there is anything in our list, if there is nothing it will check and reply and continue the app, however if we do have things in our list it will print out every item and continue !Code:else if(Choice.equals("3")){ if(AL.isEmpty()){ showMessageDialog(null,"There is nothing in the list !"); continue; } for(int i=0; i<AL.size(); i++) showMessageDialog(null,"These exists in the list"+AL.get(i)); }
The fourth option !
Here we check if the list is empty and if so just reply and continue else not then clear everything and reply then continue !Code:else if(Choice.equals("4")){ if(AL.isEmpty()){ showMessageDialog(null, "The list is already empty !"); continue; } AL.clear(); showMessageDialog(null, "The list is now empty !"); }
The fifth option !
This small task is really a neat thing about ArrayList, instead of creating an own or using someone else sorting algorithm we use the build in sorter!Code:else if(Choice.equals("5")) { Collections.sort(AL); showMessageDialog(null,"Sorted "+AL); }
*A small note !*
If you have imported only, java.util.ArrayList; then I suggest you add in
java.util.Collections; however if you imported the whole thing java.util.*; then you have nothing to worry with my notice.
Sixth option !
This part allows us to add an element in whatever position we want to, can come in handy for does who are dandy !Code:else if(Choice.equals("6")) { String position = showInputDialog(null,"Type in position"); int pos = Integer.parseInt(position); String NewElement= showInputDialog(null,"Type in an element"); AL.add(pos,NewElement); }
Seventh option !
Just closes itself nothing much.Code:else if(Choice.equals("7")){ break; }
Last part !
The whole code !Code:else break; } } }
So hopefully you guys will learn something about ArrayListsCode:import static javax.swing.JOptionPane.*; import javax.swing.*; import java.util.*; public class ArrayList { public static void main(String [] arg) { while(true){ String Choice= showInputDialog(null,"1.Add an element in the list" +"\n2.Delete an element out of the list" +"\n3.Show all elements in the list" +"\n4.Delete all elements out of the list" +"\n5.Sort" +"\n6.Add an element in a free position" +"\n7 Close"); if(Choice== null) break; else if(Choice.equals("1")){ String newElement = showInputDialog(null,"Add an element"); if(newElement == null) continue; AL.add(newElement); JOptionPane.showMessageDialog(null, "The element is now added !"); } else if(Choice.equals("2")){ AL.size(); if(AL.size()<1) { showMessageDialog(null,"You can't delete any elements there are non !"); continue; } String Del = showInputDialog("In what position? (0-"+(AL.size()-1)+")"); if(Del== null) continue; while(true){ try{ int Number= Integer.parseInt(Del); if(Number<0 || Number>= AL.size()) throw new Exception(); AL.remove(Number); break; } catch(Exception e){ Del = showInputDialog("You must chose between 0 and"+(AL.size()-1)+ "\nIn what position(0-"+(AL.size()-1)+")"); } } showMessageDialog(null, "Element is now deleted !"); } else if(Choice.equals("3")){ if(AL.isEmpty()){ showMessageDialog(null,"There is nothing in the list !"); continue; } for(int i=0; i<AL.size(); i++) showMessageDialog(null,"These exists in the list"+AL.get(i)); } else if(Choice.equals("4")){ if(AL.isEmpty()){ showMessageDialog(null, "The list is already empty !"); continue; } AL.clear(); showMessageDialog(null, "The list is now empty !"); } else if(Choice.equals("5")) { Collections.sort(AL); showMessageDialog(null,"Sorted "+AL); } String position = showInputDialog(null, "Type in position"); int pos = Integer.parseInt(position); try { String NewElement = showInputDialog(null, "Type in an element"); AL.add(pos, NewElement); } catch (IndexOutOfBoundsException err) { showMessageDialog(null,"Index "+ pos +" is out of bounds."); } } else if(Choice.equals("7")){ break; } else break; } } }
Peace out !
A picture of it at work !
![]()
Last edited by Turk4n; 01-04-2009 at 06:56 AM. Reason: Thanks chili5 !
Very nice tutorial filled with a lot of information! +rep
Nice job, +rep
Very well done indeed! The ArrayList is probably my favorite container.
I don't see why this can't be done, but can you have an array list in an array list?
In your section where you show the entire code you forgot to include the array list declaration.
Also in #6, perhaps try to stop the user from inputting a number that is out of bounds in the array list.
Code:String position = showInputDialog(null, "Type in position"); int pos = Integer.parseInt(position); try { String NewElement = showInputDialog(null, "Type in an element"); AL.add(pos, NewElement); } catch (IndexOutOfBoundsException err) { showMessageDialog(null, "Index " + pos + " is out of bounds."); }
nice one .. +rep when it lets me to![]()
Nice tutorial
A quick note, actually you can have an ArrayList of chars, you just have to use the wrapper class Character.
Code:ArrayList<Character> charArr = new ArrayList<Character>();
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks