Access ArrayList of Objects

What code do i need to have in order to access the object inside the array list and print each element from that object.

import java.io.*;

import java.util.ArrayList;

class Board

{

publicstaticvoid main (String[] args)

{

ArrayList myList =new ArrayList ();

Player p1 =new Player (100,"jack","fast");

myList.add (p1);

}

}

class Player

{

privateint score;

private String name;

private String power;

public Player (int score, String name, String power)

{

this.score = score;

this.name = name;

this.power = power;

}

}

[1419 byte] By [Wulda] at [2007-11-27 6:38:20]
# 1

private void foo(ArrayList myList){

Player player;

for(Object obj: myList){

player = (Player)obj;

System.out.println("Score: " + player.score);

System.out.println("Name: " + player.name);

System.out.println("Power: " + player.power);

}

}

Not very elegant but works... my first enhanced for loop woohoooo :-)

hope this helps...

DarumAa at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 2

If you are using Java 1.5 or above then you can do the following to prevent casting.

import java.util.ArrayList;

import java.util.List;

class Board

{

public static void main (String[] args)

{

List<Player> myList = new ArrayList<Player> (); //Specify type to save casting

Player p1 = new Player (100, "jack", "fast");

myList.add (p1);

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

}

}

class Player

{

private int score;

private String name;

private String power;

public Player (int score, String name, String power)

{

this.score = score;

this.name = name;

this.power = power;

}

public String getName() {

return name;

}

public String getPower() {

return power;

}

public int getScore() {

return score;

}

}

_helloWorld_a at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 3
Also, maybe consider expressing power with a numerical value which will be might be more flexible depending on your needs.
_helloWorld_a at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you _helloWorld_ for making my inelegant code more elegant :-)

The improved version:

private static void foo(ArrayList<Player> myList){

for(Player player: myList){

System.out.println("Score: " + player.score);

System.out.println("Name: " + player.name);

System.out.println("Power: " + player.power);

}

}

DarumAa at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 5

class Player {

// ...

public String toString() {

return "Name: " + name + "\n Power: " + power + "\n Score: " + score;

}

}

// ...

for (Player player : myList) {

System.out.println(player);

}

S_i_m_ua at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 6
Even better. I stand in awe!
DarumAa at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 7
> Even better.Yep, it is rare that I think to override the toString method to something useful.
_helloWorld_a at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 8

import java.io.*;

import java.util.ArrayList;

class Board

{

public static void main (String[] args)

{

ArrayList<Player> myList = new ArrayList<Player> ();

Player p1 = new Player (100, "jack", "fast");

myList.add (p1);

for(Player p:myList)

System.out.println(p);

}

}

class Player

{

private int score;

private String name;

private String power;

public Player (int score, String name, String power)

{

this.score = score;

this.name = name;

this.power = power;

}

public String toString() {

return ""+name+" "+score+" "+power;

}

}

eaajea at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 9

List<Player> myList = new ArrayList<Player> (); //Specify type to save casting

Player p1 = new Player (100, "jack", "fast");

myList.add (p1);

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

What are <Player> for? When i try running the program there is an error on that line.

Also, there is an error on the println ( message says, "No method named "getName" was found in type "java.lang.Object")

Wulda at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 10

> List<Player> myList = new ArrayList<Player> ();

> //Specify type to save casting

>

> Player p1 = new Player (100, "jack", "fast");

>

> st.add (p1);

>

> ystem.out.println(myList.get(0).getName());

>

>

> What are <Player> for? When i try running the program

> there is an error on that line.

>

> Also, there is an error on the println ( message

> says, "No method named "getName" was found in type

> "java.lang.Object")

I think you are using a JDK which is older than version 1.5. If that is true then use the method described in reply # 1.

If you are using JDK 1.5 or higher then please post your code and the exception being generated so someone can help you out.

Thanks.

EDIT: <Player> is an example of generics in Java. It forces a compile time checking of the type of objects you are adding to a collection like ArrayList. You can read about it more over here:

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

However, generics were introduced in Java 1.5. If you have an older JDK, generics wouldn't work.

Message was edited by:

DarumA

DarumAa at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 11

I'm using (Ready to Program 1.7 using Java 1.4.2).

I have tried the first way but there is an error so i guess i didn't use it the right way.

error: for(Object obj: myList) // says "misplaced construct(s)"

import java.util.ArrayList;

class Board

{

public static void main (String[] args)

{

ArrayList myList = new ArrayList ();

Player p1 = new Player (100, "jack", "fast");

myList.add (p1);

foo (myList);

}

private void foo(ArrayList myList)

{

Player player;

for(Object obj: myList)

{

player = (Player)obj;

System.out.println("Score: " + player.score);

System.out.println("Name: " + player.name);

System.out.println("Power: " + player.power);

}

}

}

class Player

{

private int score;

private String name;

private String power;

public Player (int score, String name, String power)

{

this.score = score;

this.name = name;

this.power = power;

}

}

Wulda at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 12

Seems like the enhanced for loop doesnt work below JDK 1.5 either. Strange I thought it was introduced in JDK1.4. Anyways below is the JDK1.4 compatible code:

import java.util.ArrayList;

import java.util.*;

class Board

{

public static void main (String[] args)

{

ArrayList myList = new ArrayList ();

Player p1 = new Player (100, "jack", "fast");

Player p2 = new Player (20, "tom", "slow");

myList.add (p1);

myList.add(p2);

foo (myList);

}

private static void foo(ArrayList myList)

{

Player player;

Iterator i = myList.iterator();

while(i.hasNext()){

player = (Player)i.next();

System.out.println("Score: " + player.score);

System.out.println("Name: " + player.name);

System.out.println("Power: " + player.power);

}

}

}

class Player

{

protected int score;

protected String name;

protected String power;

public Player (int score, String name, String power)

{

this.score = score;

this.name = name;

this.power = power;

}

}

DarumAa at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...
# 13
Thank you all for the time and help.
Wulda at 2007-7-12 18:06:57 > top of Java-index,Java Essentials,New To Java...