get values out from array list

hi i can get out values from array list but it gives me all, how can i get just one value out. i mean, i have 2 attributes for each record stored, i only want to list out all values, i mean just all the names, i hope code makes sense. i have name and age, i want name to come out for one record. the code below gets whole element out from arraylist. how would i get just one variable out, could some1 give me an example

System.out.println("value: " ++ iList.get(0)}

[529 byte] By [javaking312a] at [2007-11-26 13:54:45]
# 1

> hi i can get out values from array list but it gives

> me all, how can i get just one value out. i mean, i

> have 2 attributes for each record stored, i only want

> to list out all values, i mean just all the names, i

> hope code makes sense. i have name and age, i want

> name to come out for one record. the code below gets

> whole element out from arraylist. how would i get

> just one variable out, could some1 give me an

> example

Let me get this straight: you have some sort of a Human class and that Human class has an ArrayList containing 2 elements: a name (String), and an age (int/Integer)? Do NOT do that!

This would be a better approach:

public class Human {

private String name;

private int age;

public Human(String name, int age) {

this.name = name;

this.age = age;

}

public void setAge(int newAge) {

this.age = newAge

}

// the rest of your methods

}

You can use the setAge(...) method to increase the age of the Human.

prometheuzza at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 2
I am assuming you are have objects in the array list that have two variables. You are going to either need to modify the toString() method of the object, or get the appropriate attribute.
zadoka at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 3

> I am assuming you are have objects in the array list

> that have two variables.

>

> You are going to either need to modify the toString()

> method of the object, or get the appropriate

> attribute.

Ah I misunderstood, I think you're right: the OP is probably already storing some sort of a Human class in his/her ArrayList.

prometheuzza at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Like prometheuzz suggestedpublic class Human

{

String name;

int age;

public String getName()

{

return name;

}

public static void main(String args)

{

ArrayList list = new ArrayList();

// add a new human to list

...

System.out.println(list.get(0).getName());

}

}

I'll let you fill in the blanks.

Ted.

ted_trippina at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 5

> ...

>public static void main(String args)

> {

>ArrayList list = new ArrayList();

> // add a new human to list

>...

> System.out.println(list.get(0).getName());

>}

> I'll let you fill in the blanks.

>

> Ted.

Without using generics, it should be casted to a Human:System.out.println(((Human)list.get(0)).getName());

With the use of generics (until Java 1.5 >):ArrayList<Human> list = new ArrayList<Human>();

System.out.println(list.get(0).getName());

prometheuzza at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 6

ok i keep getting the name of the last element entered coming out

i have done this code and get last element, just the name but need to specify for example element 2 name

System.out.println("Name: " + tTeam.getName());

i do this and then get compile error as previous post implied to do

System.out.println("Name: " + tTeam.get(1).getName());

error is

symbol : method get(int)

location: class Team

System.out.println("Name: " + tTeam.get(1).getName());

^

1 error

whats the error mean

javaking312a at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 7
> ...> whats the error meanTeam does not have a method called get(int). An ArrayList does have such a method.
prometheuzza at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 8
> whats the error meanThe error means that whatever object your tTeam is does not have a method like "get(int)".
zadoka at 2007-7-8 1:33:29 > top of Java-index,Java Essentials,Java Programming...
# 9

You're clearly a complete java beginner so here goes...

import java.util.ArrayList;

public class Human

{

String name;

int age;

public Human(String name, int age)

{

this.name = name;

this.age = age;

}

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

public static void main(String[] args)

{

ArrayList<Human> list = new ArrayList<Human>();

// add a new human to list

list.add(new Human("Noob", 1));

list.add(new Human("Dave", 20));

list.add(new Human("Yoda", 999));

System.out.println(list.get(0).getName());

System.out.println(list.get(1).getName());

System.out.println(list.get(2).getName());

Human dave = list.get(1);

System.out.println(dave.getName());

}

}

Ted.

ted_trippina at 2007-7-8 1:33:30 > top of Java-index,Java Essentials,Java Programming...
# 10

> ok i keep getting the name of the last element

> entered coming out

Ah, that's a classic, that is. It most likely means you're only using one object and keep changing its contents. That way you end up with all the entries in your array list referring to the same single object.

Another way is to use static variables to contain the data for your objects, but I'm betting on the classic way. Check out ted_trippin's code for the right way to do things.

DrClapa at 2007-7-8 1:33:30 > top of Java-index,Java Essentials,Java Programming...