944,122 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 7157
  • Java RSS
Apr 13th, 2007
0

ArrayList Help

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2mdennis is offline Offline
4 posts
since Apr 2007
Apr 13th, 2007
0

Re: ArrayList Help

You should just be able to add custom objects into your arrayList as you would say strings etc.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 14th, 2007
0

Re: ArrayList Help

Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 14th, 2007
0

Re: ArrayList Help

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;
}


Click to Expand / Collapse  Quote originally posted by 2mdennis ...
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 14th, 2007 at 12:30 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
arunjain_in is offline Offline
4 posts
since Apr 2007
Apr 14th, 2007
0

Re: ArrayList Help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2mdennis is offline Offline
4 posts
since Apr 2007
Apr 14th, 2007
0

Re: ArrayList Help

can u plz send me your code....then i may be able to help you better...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
arunjain_in is offline Offline
4 posts
since Apr 2007
Apr 14th, 2007
0

Re: ArrayList Help

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);
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2mdennis is offline Offline
4 posts
since Apr 2007
Apr 14th, 2007
0

Re: ArrayList Help

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


Reputation Points: 10
Solved Threads: 1
Newbie Poster
arunjain_in is offline Offline
4 posts
since Apr 2007
Apr 15th, 2007
0

Re: ArrayList Help

Thanks for the Help, have got it working now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2mdennis is offline Offline
4 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: i m looking for help
Next Thread in Java Forum Timeline: Homework question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC