ArrayList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: ArrayList

  1. #1
    Join Date
    Dec 2005
    Posts
    27

    ArrayList

    Does anyone know how i could remove objects from an array list that are the same
    ex:
    1
    2
    4
    4
    4
    5
    changing array list values to
    1
    2
    4
    5
    Thanx for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    62
    Hi,

    Code:
    ArrayList<Integer> duplicates = new ArrayList<Integer>()
    Integer previous = new Integer(Integer.MIN_VALUE); // so it wont conflict with list values
    for(int i=0; i<arrayList.size(); i++) // arrayList is your list
    {
        // first find duplicates
        if(arrayList.get(i).equals(previous))
            duplicates.add(i);  // add the index of the duplicate
    
        else{
            previous = arrayList.get(i);
        }
    }
    
    // now remove duplicates
    for(int j= duplicates.size()-1; j>=0; j--)
    {
        arrayList.remove(duplicates.get(j));
    }
    Kind regards,
    Noel
    Efficiency is intelligent laziness

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If you were wanting to create a collection of discrete values while not getting rid of your original list, you can create an instance of CopyOnWriteArraySet using the CopyOnWriteArraySet( ArrayList yourArrayList) constructor - then iterate through the list. Or copy the elements to a collection implementing the Set interface yourself ...

  4. #4
    Join Date
    Dec 2005
    Posts
    27
    That method isn't working
    I am trying to calculate the mode I am using 2 methods
    Code:
    public static ArrayList duplicates(ArrayList modual)
        {
            Collections.sort(modual); 
            Double previous = (Double)modual.get(1); 
            ArrayList duplicate = new ArrayList();
            for(int i = 0; i < modual.size()-1; i++)
            {
                double temp = (Double) (modual.get(i)); 
                if(temp == previous)
                {
                    duplicate.add(temp); 
                }
                else
                {
                    previous = (Double) modual.get(i);
                }
            } 
            for(int j = 0; j <= duplicate.size()-1; j++)
            { 
                modual.remove(duplicate.get(j));   
            }
            return modual; 
        }
    the purpose of this code is to take in the arraylist and remove the duplicates
    Code:
        public static ArrayList mode(double array[]) throws IOException
        {
            double n;
            int freq = 0;
            int Mfreq = 0;
            double Mn = array[0];
            ArrayList modes = new ArrayList(); 
            PrintStream fout = new PrintStream( new FileOutputStream("output.txt"));
            for (int i = 0; i < array.length; i++) 
            {
                n = array[i];  
                if(i == 0 || array[i] != array[i - 1])
                { 
                    for (int j = 0; j < array.length; j++) 
                    {  
                        if (array[j] == n) 
                        { 
                            freq++;  
                        }
                    }
                }
                if(freq != 0)
                {
                    modes.add(freq+" "+array[i]);
                }
                freq = 0;
                if (freq > Mfreq) 
                {
                    Mn = n;
                }
            }
            double mode = 0.0; 
            double oc = 0.0; 
            String str [] = new String [modes.size ()];
            modes.toArray (str);
            ArrayList modual = new ArrayList(); 
            for(int i = 0; i <= modes.size() -1; i ++)
            {
                
                String temp =  (String) (modes.get(i));
                int pos = str[i].indexOf(' ') ;
                String occ = str[i].substring(0 , pos); 
                int occurances = Integer.parseInt(occ); 
                occ = str[i].substring(pos, str[i].length()); 
                double number = Double.parseDouble(occ); 
                if(occurances >= oc)
                {
                    modual.add(number);                
                    oc = occurances; 
                }   
            }
            return modual; 
        }
    the purpose of this method is to take in an array and caluclate the mode

  5. #5
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by stormswimmer
    Does anyone know how i could remove objects from an array list that are the same
    ex:
    1
    2
    4
    4
    4
    5
    changing array list values to
    1
    2
    4
    5
    Thanx for any help
    The indexOf and lastIndexOf methods will work nicely.
    Code:
    
    public ArrayList removeDuplicates(ArrayList al) {
        for (int i = 0; i < al.size(); i++) {
            while (i != al.lastIndexOf(al.get(i))) {
                al.remove(al.lastIndexOf(i));
            }
        }
        return al;
    }
    That might be a bit sloppy to read... but basically, it loops through the ArrayList. At each index, it checks if that index equals the last index with the same object. If they are the same, there's only one in there. If they do not equal, it will remove all of the duplicates.

  6. #6
    Join Date
    Dec 2005
    Posts
    27
    This method doesn't work either i get a ArrayIndexOutOfBoundsException

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Code:
    public TreeSet<Integer> removeDuplicates (ArrayList<Integer> myArray)
    {
         TreeSet<Integer> mySet = new TreeSet<Integer>();
          mySet.addAll( myArray );
          return mySet;
    }
    
    public void printSet( TreeSet<Integer> printSet )
    {
         for ( Integer i : printSet )
         {
               System.out.print( i );
         }
         System.out.println();
    }

  8. #8
    Join Date
    Dec 2005
    Posts
    27
    How does this method work?

  9. #9
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by stormswimmer
    This method doesn't work either i get a ArrayIndexOutOfBoundsException
    That's not because of my method. I just compiled and tested it; it works fine. Besides, nothing in the ArrayList class throws an ArrayIndexOutOfBoundsException; it does throw IndexOutOfBoundsExceptions though.

Similar Threads

  1. Storing ArrayList from ResultSet
    By lamagra in forum Java
    Replies: 1
    Last Post: 06-29-2005, 10:08 PM
  2. Superclass Arraylist, Subclass
    By Case-Sensitive in forum Java
    Replies: 3
    Last Post: 05-15-2005, 12:19 PM
  3. Replies: 10
    Last Post: 05-15-2005, 07:14 AM
  4. Replies: 4
    Last Post: 04-13-2005, 10:37 PM
  5. Converting arraylist to array
    By warriorwarren in forum Java
    Replies: 11
    Last Post: 03-19-2005, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center