| | |
ArrayList Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 1
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;
}
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;
}
•
•
•
•
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.
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 1
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);
}
}
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);
}
}
![]() |
Similar Threads
- Using operator[] with an ArrayList (Java)
- removing duplicates in arraylist (Java)
- Is ArrayList Better than Vector (Java)
- Storing Point2D.Double objects in an ArrayList. (Java)
- Need Help with ArrayList sorting (Java)
Other Threads in the Java Forum
- Previous Thread: i m looking for help
- Next Thread: Homework question
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






