-1

I'm continuously having problems with this computer Assignment. Essentially we have to edit the two add methods of ArrayList, so that it will allow us to store integers going from highest to lowest.

Here is the code my professor provided to test our add methods:

package Asg3;

import java.util.ArrayList;

import myUtil.SortedArrayList;

/**
 * Don't modify any of the following codes. 
 * 
 * 2/14/2016
 * 
 * @author Chung-Chih Li
 */

public class Asg3 {



    public static void testInteger() {
        SortedArrayList<Integer> sorted= new SortedArrayList<Integer>();

        for (int i=0;i<20;i++) {
            sorted.add((int)(Math.random()*1000));
        }
        int bad=0;
        for (int i=0;i<20;i++) {
            try {
                sorted.add((int)(Math.random()*1000)%sorted.size(),(int)(Math.random()*1000));
            } catch (IllegalArgumentException e) {
                System.out.print(".");
                bad++;
            }

        System.out.println("\nsize: "+sorted.size()+"  bad insertions: "+bad);
        System.out.println(sorted.toString());}}





    public static void main(String[] args) {

        testInteger();



    }

}

Here are my two add methods:

package myUtil;

public class SortedArrayList<T extends Comparable<T>>extends java.util.ArrayList<T>
{
    public SortedArrayList()
    {
        super();
    }

    public SortedArrayList(int capacity)
    {
        super();
    }

    @Override
    public boolean add(T item)
    {
        if(this.size()!=0)
        {
            int index=this.size()-1;
            //tests to see if item is greater than the last index and if so places it there
            if(item.compareTo(this.get(index))>=0)
            {
                super.add(item);
            }
            else
            {//tests to see at what index other than the last index, would be appropriate to place the item in.
                for(int i=1; i<this.size()-1;i++)
                {
                    if(item.compareTo(this.get(i-1))<=0 && item.compareTo(this.get(i+1))>=0)
                        {super.add(i,item);
                        return true;}
                    else
                        continue;
                }//fix add method
            }

        }
        else
            {super.add(0, item);}
        return true;
    }


    @Override//fix add method
    public void add(int i, T item)
    {   
        if(i==0)
        {
            if(item.compareTo(this.get(i))==0&&item.compareTo(this.get(i+1))>0)
            {
                super.add(i,item);

            }

        }
        else
        {
            try{
            if(item.compareTo(this.get(i-1))<0 && item.compareTo(this.get(i+1))>0)
            super.add(i, item); }
        catch(IndexOutOfBoundsException e){throw new IllegalArgumentException();}   
        }   





}}

Now my program compiles but the amount of integers I have stored in the arraylist is way to few. The array list should be storing 20+ integers. I know that my boolean add method is the problem.

Any help you guys could offer me is as always much appreciated.

skulltula
  • 153
  • 1
  • 3
  • 10

2 Answers2

0

I'm not 100% sure this will fix your problem, but for starters this piece of code:

if(item.compareTo(this.get(i-1))<=0 && item.compareTo(this.get(i+1))>=0)
                        {super.add(i,item);
                        return true;}

is comparing every other element in the arraylist, not two adjacent elements. You should be comparing elements i-1 to i, or i to i+1 since there exists an element at position i already.

Gordon Allocman
  • 768
  • 7
  • 23
0

You are overcomplicating your add method. If you know that the list you are adding to is sorted then you just need to find the first item that is smaller than the item you are adding and insert before it. If you get to the end of this list and none of the items are smaller then you know you need to add to the end. There's no need to compare to items above and below and there's no need for a special check for an empty list.

for (int i = 0; i < size(); i++) {
    if (item.compareTo(get(i)) < 0) {
        add(i, item);
        return;
    }
}
add(item);

I'm not sure what the boolean return value is for. Is the method supposed to detect duplicates and return false?

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • Yea I see what your saying, I definitly was making it overly complicated, thanks for the help. As for the boolean return value, our teacher just wanted it lol, honestly have no idea why. We were to override the deafulat add methods from class ArrayList which for whatever reason one of them is of type boolean and always returns true. Thank you for your help man. – skulltula Feb 29 '16 at 22:06