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

Thread: ArrayList - Simple version !

  1. #1
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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 08:56 AM. Reason: Thanks chili5 !

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: ArrayList - Simple version !

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

  3. #3
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: ArrayList - Simple version !

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    Re: ArrayList - Simple version !

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

  5. #5
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: ArrayList - Simple version !

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  6. #6
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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.");
                    }
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  7. #7
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  8. #8
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

    Re: ArrayList - Simple version !

    nice one .. +rep when it lets me to

  9. #9
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: ArrayList - Simple version !

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  10. #10
    Learning Programmer Stu_328 is on a distinguished road
    Join Date
    Dec 2008
    Posts
    85

    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
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Project: ionFiles - Joomla Simple File Download
    By Jordan in forum Community Projects
    Replies: 369
    Last Post: 01-30-2010, 01:10 PM
  2. Get Version Info
    By Xav in forum PHP Tutorials
    Replies: 15
    Last Post: 10-11-2008, 03:15 PM
  3. Probably a simple class question
    By utdiscant in forum Python
    Replies: 1
    Last Post: 06-10-2008, 09:10 PM
  4. Replies: 0
    Last Post: 03-30-2008, 10:36 PM
  5. Limit in Oracle
    By oppo in forum Database & Database Programming
    Replies: 1
    Last Post: 11-16-2007, 11:35 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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