I figured out how to output the Max/Min, sort, prompt to delete duplicates, but I can't for the life of me identify the duplicates. While trying to reuse the delete duplicate code to show the duplicates I get errors.
Is there something that I am doing slightly wrong or extremely wrong?
V/r
import java.util.*; //**all the Collections Framework classes are here
import javax.swing.*;
public class ArrayListTest {
public static void main(String[] args) {
String input, userstring;
int size;
int range;
int randInt;
int userAnswer;
//**indexed list of (any kind of) objects (but not primitives).
//**size expands as needed
ArrayList myArrayList = new ArrayList();
input = JOptionPane.showInputDialog( "Enter number of random ints to generate" );
size = Integer.parseInt( input );
input = JOptionPane.showInputDialog( "Enter upper range of the random ints" );
range = Integer.parseInt( input );
for (int i=1; i<=size; i++) { //i is not an index, just a loop counter
randInt = (int)(Math.random()*range); //random int between 0 and range-1
//uncomment if this code confuses you:
//System.out.println("generated int: " + randInt);
myArrayList.add(new Integer(randInt)); //**must wrap int into Integer
}
//**iterate over all the objects in the arraylist collection.
Iterator it=myArrayList.iterator();
//**an iterator is for accessing all the objects of collection, one by one.
//**hasNext() is true until reach end of the collection.
//**next() returns the next object in the iteration.
//**notice no for loop looping over indexes!
System.out.println("Here are your random integers: ");
while (it.hasNext())
//display them on one line, separated by a blank
System.out.print(it.next() + " ");
System.out.println();
//Used to Identify and Display duplicate integers
// This is where I am having trouble displaying duplicate elements. What am I doing wrong???
Collections.sort(myArrayList);
it=myArrayList.iterator();
while (it.hasNext())
if (it(i)) == (it(i-1))) {
System.out.print();
}
//Used to prompt user if they want to delete the duplicate integers
userAnswer = JOptionPane.showConfirmDialog(null,
"Do you want to delete duplicate integers in your list?",
"Delete 'em?",
JOptionPane.YES_NO_OPTION);
if (userAnswer == JOptionPane.YES_OPTION) {
Collections.sort(myArrayList);
it=myArrayList.iterator();
Set uniqueEntries = new HashSet();
for (int i=1; i<=size; i++) {
Object element = it.next();
if (!uniqueEntries.add(element)) // if current element is a duplicate,
it.remove(); // remove it
}
}
//** sort
Collections.sort(myArrayList);
it=myArrayList.iterator();
System.out.println();
System.out.println("Here are your sorted integers: ");
while (it.hasNext())
System.out.print(it.next() + " ");
System.out.println();
//** smallest and largest values
System.out.println("Min value in arraylist: " + Collections.min(myArrayList));
System.out.println("Max value in arraylist: " + Collections.max(myArrayList));
}
}
This post has been edited by blackcompe: 04 February 2012 - 01:31 AM

New Topic/Question
Reply



MultiQuote









|