+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: ArrayList - Simple version !

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    ArrayList - Simple version !

    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 !
    Code:
    else
    break;
    }
    }
    }
    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 !
    ArrayList - Simple version !-test.png
    Last edited by Turk4n; 01-04-2009 at 06:56 AM. Reason: Thanks chili5 !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: ArrayList - Simple version !

    Very nice tutorial filled with a lot of information! +rep

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: ArrayList - Simple version !

    Quote Originally Posted by Jordan View Post
    Very nice tutorial filled with a lot of information! +rep
    Thanks !

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: ArrayList - Simple version !

    Nice job, +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: ArrayList - Simple version !

    Quote Originally Posted by WingedPanther View Post
    Nice job, +rep
    Thank you

  7. #6
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: ArrayList - Simple version !

    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.");
                    }

  8. #7
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: ArrayList - Simple version !

    Quote Originally Posted by chili5 View Post
    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.");
                    }
    Thanks, well I should have checked the sixth option :>
    Indeed you can add a ArrayList in another ArrayList ! But not the same list itself

  9. #8
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: ArrayList - Simple version !

    nice one .. +rep when it lets me to

  10. #9
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: ArrayList - Simple version !

    Quote Originally Posted by Egz0N View Post
    nice one .. +rep when it lets me to
    Thank you Egz0n !

  11. #10
    Stu_328 is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    92
    Rep Power
    12

    Re: ArrayList - Simple version !

    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>();

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. No output on simple code: learning ArrayList class
    By scottbomb in forum Java Help
    Replies: 3
    Last Post: 06-08-2011, 06:46 PM
  2. JComboBox - Simple Version !
    By Turk4n in forum Java Tutorials
    Replies: 9
    Last Post: 08-07-2009, 10:22 PM
  3. CodeCall's First Applet - Simple Version !
    By Turk4n in forum Java Tutorials
    Replies: 13
    Last Post: 01-15-2009, 01:09 AM
  4. Abstract method(SIMPLE VERSION)
    By Turk4n in forum Java Tutorials
    Replies: 2
    Last Post: 09-20-2008, 12:35 AM
  5. Tutorial paint dots(simple version)
    By Turk4n in forum Java Tutorials
    Replies: 6
    Last Post: 06-25-2008, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts