User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,358 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2004 | Replies: 8 | Solved
Reply
Join Date: Apr 2007
Posts: 4
Reputation: 2mdennis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2mdennis 2mdennis is offline Offline
Newbie Poster

Help ArrayList Help

  #1  
Apr 13th, 2007
I am fairly new to Java and am having some difficulty with implementing an arraylist.

I have some classes that contain variables and i have a new class that contains my arraylist. I would like to know how i would get the variables from the other classes into the arraylist in the new class. I know that you would use the add methods, but am unsure of the code to do it.

Example code would be very useful.

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 309
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: ArrayList Help

  #2  
Apr 13th, 2007
You should just be able to add custom objects into your arrayList as you would say strings etc.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,812
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: ArrayList Help

  #3  
Apr 13th, 2007
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: arunjain_in is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
arunjain_in arunjain_in is offline Offline
Newbie Poster

Solution Re: ArrayList Help

  #4  
Apr 13th, 2007
Hi,
if i have got your problem write....this can be the solution.

I have 2 classes A and B and I am adding its variable to arraylist in third class. Take care of access modifiers. Also objects can be added to arraylist like wrapper, string or custom objects along with primitive variables.
<----Array List class----->
import java.util.ArrayList;

publicclass arraylisttest {

public static void main(String[] args) {
ArrayList al = new ArrayList();
A a = new A();
B b = new B();
al.add(A.i);
al.add(B.d);
al.add(A.ii);

}

}

<----Class A---->
publicclass A{
public int ii;
public Integer i;
}

<-----Class B----->
publicclass B{

public Double d;
}


Originally Posted by 2mdennis View Post
I am fairly new to Java and am having some difficulty with implementing an arraylist.

I have some classes that contain variables and i have a new class that contains my arraylist. I would like to know how i would get the variables from the other classes into the arraylist in the new class. I know that you would use the add methods, but am unsure of the code to do it.

Example code would be very useful.

Thanks
Last edited by arunjain_in : Apr 13th, 2007 at 11:30 pm.
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: 2mdennis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2mdennis 2mdennis is offline Offline
Newbie Poster

Help Re: ArrayList Help

  #5  
Apr 14th, 2007
Thanks for the Help.

However i forgot to mention that the classes that contain the variables, are in constructors. And now i am getting errors saying 'cannot find symbol - constructor classA'. What can i do to overcome this error.

Thanks
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: arunjain_in is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
arunjain_in arunjain_in is offline Offline
Newbie Poster

Re: ArrayList Help

  #6  
Apr 14th, 2007
can u plz send me your code....then i may be able to help you better...
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: 2mdennis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2mdennis 2mdennis is offline Offline
Newbie Poster

Help Re: ArrayList Help

  #7  
Apr 14th, 2007
HI, this is the code for my first class, which contains the variables and constructor. Then i have several other classes that contain the same kind of code:

import java.util.Scanner;

public class Details
{
Scanner console = new Scanner(System.in);

public String name;
public int age;

public Details(String Name, int Age)
{
name = Name;
age = Age;
}

public String getname()
{
return name;
}

public int getage()
{
return age;
}

Then i have the class that contains the arraylist:

import java.util.ArrayList;

public class arrayList
{
public static void main(String[] args)
{
List<Details> list = new ArrayList<Details>();

Details a = new Details();
list.add(Details.name);
list.add(Details.age);

System.out.println(list);
}
}
Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: arunjain_in is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
arunjain_in arunjain_in is offline Offline
Newbie Poster

Re: ArrayList Help

  #8  
Apr 14th, 2007
try this.....actually the problem was....u were instantiating ur Details classes with passing any argument and in your Details clas u didnt had any such constructor defined.....now i hope this will work fine.

import java.util.Scanner;

public class Details {
Details(){

}
Scanner console = new Scanner(System.in);

public String name;

public int age;

public Details(String Name, int Age) {
name = Name;
age = Age;
}

public String getname() {
return name;
}

public int getage() {
return age;
}
}


import java.util.ArrayList;
import java.util.List;

public class arrayList
{
public static void main(String[] args)
{
List list = new ArrayList();

Details a = new Details();
list.add(a.name);
list.add(a.age);

System.out.println(list);
}
}


Reply With Quote  
Join Date: Apr 2007
Posts: 4
Reputation: 2mdennis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2mdennis 2mdennis is offline Offline
Newbie Poster

Re: ArrayList Help

  #9  
Apr 15th, 2007
Thanks for the Help, have got it working now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC