INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help learning how to traverse objects in and arraylist.

Need help learning how to traverse objects in and arraylist.

Need help learning how to traverse objects in and arraylist.

(OP)
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

RE: Need help learning how to traverse objects in and arraylist.

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

RE: Need help learning how to traverse objects in and arraylist.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close