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 !
Code:
import static javax.swing.JOptionPane.*;
import javax.swing.*;
import java.util.*;
public class ArrayListTest {
public static void main(String [] arg) {
Now I chose to import the whole library than just one of the book(just a
metaphor)
To get only what we want to use, the import would look like this !
Code:
import java.util.ArrayList;
A small note !, when you import and close an import with ".*;" equals you
import the whole library.
Our ArrayList.
Code:
ArrayList<String> AL = new ArrayList<String>();
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 !
So now let's go on with our options.
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;
This will force our user to make a choice :>
On with the first option !
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 will automatically add an element in the first position, so what did we do exactly?
Code:
AL.add(newElement);
Here we worked in by adding our element; but will this really work?
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.
Code:
AL.add(Position,your element);
How it looks when we implement it in
Code:
AL.add(0,newElement);
So let's go on with our second option !
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 !");
}
Here is kind of a lot codes so let's make it simple and understandable !
We check our List if it contains anything, since we can't delete something with nothing in?
And that part is....
Code:
if(AL.size()<1) {
showMessageDialog(null,"You can't delete any elements there are non !");
Next thing to explain, this part
Code:
String Del = showInputDialog("In what position? (0-"+(AL.size()-1)+")");
if(Del== null) continue;
while(true){
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 !
However we want to delete so let's check it !
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 !");
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 !
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 !
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));
}
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 !
The fourth option !
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 !");
}
Here we check if the list is empty and if so just reply and continue else not then clear everything and reply then continue !
The fifth option !
Code:
else if(Choice.equals("5")) {
Collections.sort(AL);
showMessageDialog(null,"Sorted "+AL);
}
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!
*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 !
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);
}
This part allows us to add an element in whatever position we want to, can come in handy for does who are dandy !
Seventh option !
Code:
else if(Choice.equals("7")){
break;
}
Just closes itself nothing much.
Last part !
The whole code !
Code:
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;
}
}
}
So hopefully you guys will learn something about ArrayLists

Peace out !
A picture of it at work !
