Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help learning how to traverse objects in and arraylist.

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
I am trying to learn how to print out an array of objects;
I can print the collection such as the followign.

MyTest.arrestDateStrList2@140c281
MyTest.arrestDateStrList2@a1d1f4
MyTest.arrestDateStrList2@1df280b

How do I print out the items in each.
arrestdate: Thu Jan 07 00:00:00 PST 1999
offensedate: Sun Feb 07 00:00:00 PST 1999
arrestdate: Sun Mar 07 00:00:00 PST 1999
offensedate: Wed Apr 07 00:00:00 PDT 1999


arrestDateStrList2.java

import java.util.Date;
public class arrestDateStrList2 {
private Date arrestDate;
private Date offenseDate;

public arrestDateStrList2(Date arrestDate, Date offenseDate) {
this.arrestDate = arrestDate;
this.offenseDate = offenseDate;
}

public Date getArressDate() { return arrestDate; }
public Date getOffenseDate() { return offenseDate; }
}

myArrayList.java

package MyTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class myArrayLists {
public static void main(String [] args) {
Date newDate = new Date(99,0,7);
Date newDate2 = new Date(99,1,7);
Date newDate3 = new Date(99,2,7);
Date newDate4 = new Date(99,3,7);
Date newDate5 = new Date(99,2,7);
Date newDate6 = new Date(99,3,7);

System.out.println(" date: " + newDate);
System.out.println(" date2: " + newDate2);
System.out.println(" date3: " + newDate3);
System.out.println(" date4: " + newDate4);


ArrayList<arrestDateStrList2> collection = new ArrayList<arrestDateStrList2>();
arrestDateStrList2 yeah = new arrestDateStrList2(newDate,newDate2);
collection.add(yeah);

// size() tells me how many slots are in the list
System.out.println("number of slots: " + collection.size());

arrestDateStrList2 sesameSt = new arrestDateStrList2(newDate3,newDate4);
collection.add(sesameSt);
// size() tells me how many slots are in the list
System.out.println("number of slots: " + collection.size());

arrestDateStrList2 third = new arrestDateStrList2(newDate5,newDate6);
collection.add(third);
// size() tells me how many slots are in the list
System.out.println("number of slots: " + collection.size());

System.out.println("now using iterator");
Iterator itr = collection.iterator();

//use hasNext() and next() methods of Iterator to iterate through the elements
System.out.println("Iterating through ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());

}
}

Howard
 
posted it to soon.. I added the following and it worked..

for (int i = 0; i < collection.size(); i++){

// Date test = collection.get(i);
System.out.println("Item " + i + " : " + collection.get(i) );
System.out.println("You were arrested on : " + collection.get(i).getArressDate());
System.out.println("Your offense was on : " + collection.get(i).getOffenseDate());

}

// for(String item: stringList){
// System.out.println("retrieved element: " + item);
}
 
Often there is no need for the index to be known/printed, so a little nicer could be smth like:

Code:
for (arrestDateStrList2 item : collection) {
  System.out.println("You were arrested on : " + item.getArressDate());
  System.out.println("Your offense was on : " + item.getOffenseDate()); 
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top