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
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