1 Replies - 53 Views - Last Post: 07 February 2012 - 07:53 PM Rate Topic: -----

Topic Sponsor:

#1 smithjr5  Icon User is offline

  • New D.I.C Head

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

Contains() and remove() methods within an arraylist

Posted 07 February 2012 - 07:47 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);
    }
}

This post has been edited by smohd: 07 February 2012 - 07:50 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Contains() and remove() methods within an arraylist

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Contains() and remove() methods within an arraylist

Posted 07 February 2012 - 07:53 PM

Duplicate post. Please dont open duplicate post for the same thread.
*Topic Closed*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1