Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-2009, 05:45 PM
Turk4n's Avatar   
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 19
Posts: 2,714
Blog Entries: 4
Rep Power: 29
Turk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to behold
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Default 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
__________________
Quote:
Originally Posted by Turk4n
The Last man who laughs, he did not get the joke.

Last edited by Turk4n; 01-04-2009 at 08:56 AM.. Reason: Thanks chili5 !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-03-2009, 09:29 PM
Jordan's Avatar   
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 18,359
Blog Entries: 90
Rep Power: 20
Jordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of light
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Default Re: ArrayList - Simple version !

Very nice tutorial filled with a lot of information! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-04-2009, 02:34 AM
Turk4n's Avatar   
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 19
Posts: 2,714
Blog Entries: 4
Rep Power: 29
Turk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to behold
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Default Re: ArrayList - Simple version !

Quote:
Originally Posted by Jordan View Post
Very nice tutorial filled with a lot of information! +rep
Thanks !
__________________
Quote:
Originally Posted by Turk4n
The Last man who laughs, he did not get the joke.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-04-2009, 06:35 AM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: ArrayList - Simple version !

Nice job, +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-04-2009, 07:14 AM
Turk4n's Avatar   
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 19
Posts: 2,714
Blog Entries: 4
Rep Power: 29
Turk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to behold
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Default Re: ArrayList - Simple version !

Quote:
Originally Posted by WingedPanther View Post
Nice job, +rep
Thank you
__________________
Quote:
Originally Posted by Turk4n
The Last man who laughs, he did not get the joke.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-04-2009, 07:52 AM
chili5's Avatar   
Code Warrior
 
Join Date: Mar 2008
Posts: 5,462
Blog Entries: 3
Rep Power: 49
chili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to behold
Default 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.");
                }
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-04-2009, 08:54 AM
Turk4n's Avatar   
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 19
Posts: 2,714
Blog Entries: 4
Rep Power: 29
Turk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to behold
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Default 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
__________________
Quote:
Originally Posted by Turk4n
The Last man who laughs, he did not get the joke.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-04-2009, 01:09 PM
Egz0N's Avatar   
Guru
 
Join Date: Sep 2008
Location: Kosovo
Age: 17
Posts: 3,822
Rep Power: 32
Egz0N is just really niceEgz0N is just really niceEgz0N is just really niceEgz0N is just really niceEgz0N is just really nice
Send a message via MSN to Egz0N
Default Re: ArrayList - Simple version !

nice one .. +rep when it lets me to
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-04-2009, 01:48 PM
Turk4n's Avatar   
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 19
Posts: 2,714
Blog Entries: 4
Rep Power: 29
Turk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to beholdTurk4n is a splendid one to behold
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Default Re: ArrayList - Simple version !

Quote:
Originally Posted by Egz0N View Post
nice one .. +rep when it lets me to
Thank you Egz0n !
__________________
Quote:
Originally Posted by Turk4n
The Last man who laughs, he did not get the joke.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-07-2009, 05:58 PM
Learning Programmer
 
Join Date: Dec 2008
Posts: 82
Rep Power: 3
Stu_328 is on a distinguished road
Default 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>();
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 360 05-23-2009 01:02 PM
Get Version Info Xav PHP Tutorials 15 10-11-2008 03:15 PM
Probably a simple class question utdiscant Python 1 06-10-2008 09:10 PM
Problem removing from ArrayList (affects Vector as well) chunkit Java Help 0 03-30-2008 10:36 PM
Limit in Oracle oppo Database & Database Programming 1 11-16-2007 11:35 AM


All times are GMT -5. The time now is 09:15 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads