3 Replies - 443 Views - Last Post: 08 February 2012 - 06:22 AM Rate Topic: -----

#1 smithjr5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-February 12

Using Remove and Contains methods on Iterators within an array list

Posted 07 February 2012 - 09:53 PM

I am trying to modify the ArrayListofDates so that after generating the random dates it loops thru the Dates and checks to see if it is already in the arraylist. If there is a duplicate it then should prompt the user if they want to remove that date from the array. I am trying to use the contains and remove method of array list. I got a good start but can't figure out the contains and remove methods to prompt the deletion of the duplicate dates. Here is what I have written so far.
import java.util.*;
import javax.swing.*;

public class ArrayListofDates {
    public static void main(String[] args) {
	String input;
	int size;
	int userAnswer;

	ArrayList myDatesList = new ArrayList();

	input = JOptionPane.showInputDialog( "Enter number of random dates" );
	size = Integer.parseInt( input );

	//**array list of random Dates
	for (int i=1; i<=size; i++) {
	    myDatesList.add(generateRandomDate());  //**pseudo data...
	}

	//iterate over them
	Iterator it=myDatesList.iterator();
	System.out.println();
	System.out.println("Here are your dates: ");

	Date thisDate;
	while (it.hasNext()) {
	    //reference to the Date in the list.
	    //***next() returns Object, must cast to Date:
	    thisDate = (Date)(it.next());
	    //thisDate = (it.next());  	  //**failure to cast would be compile error
	    System.out.print(thisDate);
	    //***access a method of the Date object:
	    System.out.println("     Month is: " + thisDate.getMonth());
	}
	System.out.println();



    	//Used to Identify and Display duplicate Dates
	//Used to prompt user if they want to delete the duplicate Dates
        //This is where I am having trouble inserting the methods.



        

    //generate a random Date.  Note this is not a method of the Date class.
    //it creates and returns a Date object.
    public static Date generateRandomDate() {
	int year, month, day=0;

	year = (int)(Math.random()*20+1995);  // +/- 10 years from 2005
    	month = (int)(Math.random()*12+1);
	switch (month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	    day = (int)(Math.random()*31+1);
	    break;
	case 4:
	case 6:
	case 9:
	case 11:
	    day = (int)(Math.random()*30+1);
	    break;
	case 2:
	    if (year%4==0 && year%100!=0 || year%400==0)  //leap year test
  	        day = (int)(Math.random()*29+1);
	    else
  	        day = (int)(Math.random()*28+1);
	    break;
	}
	//instantiate a Date object using the 3-arg constructor,
	//then return the object to caller.
        return new Date(month,day,year);
    }
}


I am currently living in a tent in SW Asia and nobody around here can help me.
Thank You!

Is This A Good Question/Topic? 0
  • +

Replies To: Using Remove and Contains methods on Iterators within an array list

#2 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Using Remove and Contains methods on Iterators within an array list

Posted 07 February 2012 - 10:43 PM

I would try a nested for loop:
 
for(int i = 0; i < myListDate.size(); i++) {
   for(int j = 0; j < myListDate.size(); j++) {
      if(myListDate.get(i).equals(myListDate.get(j)) {
         //prompt user if want to delete and use remove() 
               //to remove that element
}
}
}


This post has been edited by illuss: 07 February 2012 - 10:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 smithjr5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-February 12

Re: Using Remove and Contains methods on Iterators within an array list

Posted 08 February 2012 - 06:09 AM

I think that I am getting closer. I would really like to know what I am doing wrong.
	//Used to prompt user if they want to delete the duplicate Dates
        for (int i=1; i<=size; i++) {
            Object element = (Date)(it.next());
            if(myDatesList.get(i).equals(myDatesList.get(it.hasNext()))) {
                   userAnswer = JOptionPane.showConfirmDialog(null,
						   element + " Is a duplicate date in your list. Do you want to delete this duplicate?",
						   "Delete the Duplicate?",
						   JOptionPane.YES_NO_OPTION);
	        if (userAnswer == JOptionPane.YES_OPTION) {
	            it.remove();	// remove it

                }
            }
        }


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,111
  • Joined: 27-December 08

Re: Using Remove and Contains methods on Iterators within an array list

Posted 08 February 2012 - 06:22 AM

Please don't open duplicate threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1