14 Replies - 289 Views - Last Post: 06 December 2012 - 01:51 PM Rate Topic: -----

#1 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Clearing an array...

Posted 06 December 2012 - 03:10 AM

hi,

I am trying to clear an array however it does not satisfy all the test conditions...

the code that i wrote to clear this array is:

	public void clear()
	{
		for(int i = 0; i < itemArray.length; i ++)
		{
			itemArray[i] = null;
			numberOfItems = -1;
			//TODO check why the clear a filled array is not working?
		}
	}



the test code is:

	public double testClear(double markpool) {
		
		int noTest = 4;
		double gain = markpool/noTest;
		double mark = 0;
		//clear an empty array
		//clear a filled array
		//clear then readd
		//clear a sorted array, test if still sort
		//clear a non sorted array;
		
		header("Testing clear");
	
		try {
			
			ResizableArray  test1 = new ResizableArray (false, 20);
			
			print ("*** Test 1: clear an emtpy array");
			test1.clear();
			
			if (test1.numberOfItems() == 0) {
				mark += gain;
				print ("*** Passed: clear empty array");
				printM (gain,mark);
			}
			print ("");
			print ("*** Test 2: clear a filled array");
			
			for (int i = 0; i < items.length; i++ ) {
				test1.addItem(items[i]);
			}
			
			
			test1.clear();
			
			if (test1.numberOfItems() == 0) {
				mark += gain;
				print ("*** Passed: clear filled array");
			}
			
			//re add:
			for (int i = 0; i < items.length; i++ ){
				test1.addItem (items[i]);
			}
			
			if (test1.numberOfItems() == items.length) {
				mark += gain;
				print ("*** Passed: re-add okay after clear");
				printM (gain, mark);
			}
			
			print ("");
			print ("** Test 3: Clear sorted and non sorted arrays");
			
			ResizableArray test2 = new ResizableArray (false, 20);
			ResizableArray test3 = new ResizableArray (true, 20);
			
			for (int i = 0 ; i < items.length; i ++ ) {
				test2.addItem(items[i]);
				test3.addItem(items[i]);
			}
			
			
			test2.clear();
			test3.clear ();
			
			if (!test2.isSorted() && test3.isSorted()) {
				mark += gain;
				print ("*** Passed: clear() doesn't change sorted variable");
				printM (gain, mark);
			}
			
			
						
		}
		catch (Exception e) {
			print ("*** Unknown exception");
			e.printStackTrace();
			return 0;
		}
		
		
		print80stars();
		return mark;
	}



the output i get is:
********************************************************************************
Testing clear
********************************************************************************
*** Test 1: clear an emtpy array
*** Passed: clear empty array
**** Gain: 2.50 . Mark: 2.50


*** Test 2: clear a filled array
*** Unknown exception

Am i missing something?

Is This A Good Question/Topic? 0
  • +

Replies To: Clearing an array...

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: Clearing an array...

Posted 06 December 2012 - 03:22 AM

This isn't enough code to reliably pinpoint the problem. You should post a minimal, but complete code sample where complete means that it compiles and can be run to reproduce the error you're encountering.

I suspect the exception occurs in the addItem method, which is thrown of by the fact the numberOfItems has the non-sensible value -1. But without seeing the code that's just a guess.
Was This Post Helpful? 0
  • +
  • -

#3 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Clearing an array...

Posted 06 December 2012 - 03:53 AM

the program compiles fine... but each method is tested throughout the program..

and u assumed right... i do get an exception with my addItem method where it tries to add item to the array outside the capacity of the array.. thats my add item method:


public boolean addItem(Item newItem)
	{
		//check to see if the item is already added to the array
		for (int i = 0; i < this.numberOfItems; i ++)
		{
			if(itemArray[i].getKey().equals(newItem.getKey()))
			{
				return false;
			}
		}
		
		if(newItem != null && newItem.getKey() != null)
		{
			if(numberOfItems == itemArray.length)
			{
				increaseArray();
				return true;
			}
			itemArray[numberOfItems] = newItem;
			numberOfItems++;
			
			if(isSorted())
			{
				sortTheArray();
			}
			return true;
		}
	
		return false;
		//TODO
	}



when the test is run it tries to add an item to the array outside its capacity and its meant to add multiple item, sorted array in sorted order. (the test never gets to this point)

This post has been edited by theNoob: 06 December 2012 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Clearing an array...

Posted 06 December 2012 - 03:56 AM

Show us your Item class.
Was This Post Helpful? 0
  • +
  • -

#5 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Clearing an array...

Posted 06 December 2012 - 03:57 AM

thats the item class.

public class Item 
{
	private String key;
	private Object data;

	public Item(String key, Object data)
	{
		this.key = key;
		this.data = data;
	}
	
	public String getKey()
	{
		return key;
	}
	
	public Object getData()
	{
		return data;
	}
	
	public String toString()
	{
		return key + ", " + data;
	}
	
	public int compareTo(Item other)
	{
		 return getKey().compareToIgnoreCase(other.getKey());
	}
	
	public boolean equals(Item other)
	{
		return this.key.equals(other.key);
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Clearing an array...

Posted 06 December 2012 - 04:11 AM

Could you describe step by step what the addItem(Item) method is meant to do? what checks should be performed etc.

Here is the fixed Item class:



public class Item implements Comparable<Item>
{
	private String key;
	private Object data;

	public Item(String key, Object data)
	{
		this.key = key;
		this.data = data;
	}
	
	public String getKey()
	{
		return key;
	}
	
	public Object getData()
	{
		return data;
	}
	
	public String toString()
	{
		return key + ", " + data;
	}
	
        @Override
	public int compareTo(Item other)
	{
		 return getKey().compareToIgnoreCase(other.getKey());
	}
	
        @Override
	public boolean equals(Object other)
	{
            if(this == other)return true;
            if(other.getClass() != this.getClass()) return false;
            
            Item item = (Item)other;
            return item.getKey().equals(this.getKey());
	}

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + Objects.hashCode(this.key);
        return hash;
    }
}


This post has been edited by darek9576: 06 December 2012 - 04:12 AM

Was This Post Helpful? 0
  • +
  • -

#7 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Clearing an array...

Posted 06 December 2012 - 04:16 AM

yeah sure, the checks it mean to perform are:

1. check to make sure that there are no duplicate keys present in the array before attempting to add, its there is, its meant to return false.

2. check if the array is full, if its full, its meant to double the size of the array and move the old array contents into the new array.

3. if the array is sorted, it will find the correct location (using the key from the item) and put the item into that location. It will than move the rest of the array elements along by one.

4. if the array is unsorted, its meant to append this item after the last data item on the array.
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: Clearing an array...

Posted 06 December 2012 - 04:31 AM

View PosttheNoob, on 06 December 2012 - 11:53 AM, said:

the program compiles fine...


The program you have on your computer might compile fine. The code you posted here definitely does not. So while you might be able to compile, run and debug your program, I am not - which was my point.

Quote

and u assumed right... i do get an exception with my addItem method where it tries to add item to the array outside the capacity of the array..


Right. Specifically it tries to add the item at the index -1 (which is an invalid index no matter how big the array is) as that's the value of numberOfItems, which, as I said, makes no sense.
Was This Post Helpful? 0
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Clearing an array...

Posted 06 December 2012 - 04:32 AM

Show us the increaseArray() method and if the array is full, (the 2nd bullet point) - is the item still meant to be added or true should be returned. In your code, you return true.
Was This Post Helpful? 0
  • +
  • -

#10 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Clearing an array...

Posted 06 December 2012 - 04:38 AM

I modified my item array code so it not longer returns true..

this is the new addItem code
	public boolean addItem(Item newItem)
	{
		//check to see if the item is already added to the array
		for (int i = 0; i < this.numberOfItems; i ++)
		{
			if(itemArray[i].getKey().equals(newItem.getKey()))
			{
				return false;
			}
		}
		
		if(newItem != null && newItem.getKey() != null)
		{
			if(numberOfItems == itemArray.length)
			{
				increaseArray();
			}
			itemArray[numberOfItems] = newItem;
			numberOfItems++;
			
			if(isSorted())
			{
				sortTheArray();
			}
			return true;
		}
		return false;
		//TODO
	}


	private void increaseArray()
	{
		//Checks to see if the array is full.
		if(numberOfItems == itemArray.length)
		{
			//creates a new array which is double the size of the old array.
			Item [] newItemArray = new Item[itemArray.length * 2];
			
			//iterates through the loop
			for (int i = 0; i < newItemArray.length; i++)
			{
				//moves it iterm in the old array into the new array.
				newItemArray[i] = itemArray[i];
			}
		}
	}


This post has been edited by theNoob: 06 December 2012 - 04:44 AM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Clearing an array...

Posted 06 December 2012 - 04:51 AM

There are some logic error in that code

		//check to see if the item is already added to the array
		for (int i = 0; i < this.numberOfItems; i ++)
		{
			if(itemArray[i].getKey().equals(newItem.getKey()))
			{
				return false;
			}
		}


First, a peformance issue and bad coding practice.
If there are 50000 items in your list, no need to call newItem.getKey() 5000 times
Imagine,in the worts case, that the getKey() process is a long process that goes an access a database...you would be loosing a lot a cycles for nothing. Avoid to call a method ,more than once so should be
		//check to see if the item is already added to the array
		String key = newItem.getKey();
		for (int i = 0; i < this.numberOfItems; i ++)
		{
			if(itemArray[i].getKey().equals(key))
			{
				return false;
			}
		}



Now the following condition could never happen

	
		if(newItem != null && newItem.getKey() != null)
		{


if newItem is null your preceeding for() loop will have already crash on a null pointer Exception when you did your newItem.getKey()

Happy coding
Was This Post Helpful? 0
  • +
  • -

#12 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Clearing an array...

Posted 06 December 2012 - 05:12 AM

Your increasArray() method is wrong.
Imagine you have the array A of length 10. Then you call increaseArray() to double its size and create a new array B.

Inside your method you do: (lets say A = 10, B is doubled, therefore 20)

for every index in B (that is 20 times) do the following:
take a number out of A and assign it to corresponding B.

But A is much shorter than B, that is why you need to loop only 10 times - lenght of A.

Then, another problem is that your method should actually return a new array with contents of A already there. That is why:

public static boolean addItem(Item item, Item[] items){
        //1 - check for duplicates
        for(int i = 0; i < items.length; i++){
            if(item.equals(items[i])) 
                return false;
        }
        
        //2 - check for size
        Item[] items2 = null;
        if(items.length >= numberOfItems){
            items2 = increaseArray(items);
        }
        
    }
    
    public static Item[] increaseArray(Item[] items){
        
        //create a new array of size 2 * items
        int len = items.length;
        Item[] array = new Item[len * 2];
        
        //copy old into new
        for(int i=0; i < len; i++){
            array[i] = items[i];
        }
        
        //return the new array
        return array;
    }



To do number 3 - post your isSorted() method.
Was This Post Helpful? 0
  • +
  • -

#13 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Clearing an array...

Posted 06 December 2012 - 05:16 AM

Thanks pbl, for explaining to me the addItem method, i now understand how the code would have bad performance...


darek9576 this is my isSorted() method..


	public boolean isSorted() 
	{
		if(sorted == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#14 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Clearing an array...

Posted 06 December 2012 - 05:43 AM

You need to refine the logic of the method ive written so that the correct statements will be executed whenever you want.


    public static boolean addItem(Item item, Item[] items){
        //1 - check for duplicates
        for(int i = 0; i < items.length; i++){
            if(item.equals(items[i])) 
                return false;
        }
        
        //2 - check for size
        Item[] items2 = null;
        if(items.length >= numberOfItems){
            items2 = increaseArray(items);
        }
        
        //if not sorted append to the end
        if(!isSorted()){
            items2[items.length] = item;
        }else
        {
            //create the insert method
            insertItem(items2, item);
        }
    }
    
    public static Item[] increaseArray(Item[] items){
        
        //create a new array of size 2 * items
        int len = items.length;
        Item[] array = new Item[len * 2];
        
        //copy old into new
        for(int i=0; i < len; i++){
            array[i] = items[i];
        }
        
        //return the new array
        return array;
    }
    
    


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Clearing an array...

Posted 06 December 2012 - 01:51 PM

View PosttheNoob, on 06 December 2012 - 08:16 AM, said:

Thanks pbl, for explaining to me the addItem method, i now understand how the code would have bad performance...


darek9576 this is my isSorted() method..


	public boolean isSorted() 
	{
		if(sorted == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


What about

   public boolean isSorted() {
       return sorted;
   }


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1