Results 1 to 6 of 6
  1. #1
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default trouble in removing a value

    here is my code
    Java Code:
    import java.util.Arrays;
    public class IntBag {
    	// PROPERTIES
    	int[] store;
    	int count;
    	int increasing;
    	
    	//CONSTRUCTORS
    	
    	// default constructor
    	public IntBag(){
    		store = new int[16];
    		count = 0;
    		increasing=16;
    	}
    	
    	// start with another size and set count to 0 and set increasing to 16
    	public IntBag(int size){
    		store = new int[size];
    		count = 0;
    		increasing=16;
    	}
    	
    	//create a new store set count to 0 and set increasing to incAmount
    	public IntBag(int size,int incAmount){
    		store = new int[size];
    		count = 0;
    		increasing = incAmount;
    	}
    	
    	//METHODS	
    	//add a new element to store
    	public void add( int newValue){
    		if( count == store.length)
    		
    			increaseSize(increasing);
    		
    		store[count]=newValue;
    		count=count+1;
    	}
    
    	// increase the size of array when there is not any space
    	private void increaseSize(int increasing){
    		int newSize;
    		if(increasing>0)
    			newSize=store.length+increasing;
    		else
    			 newSize = store.length * 2;
    		int[] temp = new int[newSize];
    		
    		for( int i = 0; i < store.length; i++)	
    			temp[i] = store[i];
    		store = temp;
    	}
    	
    	// get the size of the store array
    	public int size(){
    		return count ;	
    	}
    	
    	//remove specified value from constructor
    	public void remove( int value) {
    		for(int i=0; i<count;i++)
    			if(store[i]==value){
    				
    				count=count-1;
    				store[i]=store[count];
    				store[count]=0;
    			}
    	}
    	
    	// test whether the collection contains the value or not
    	public boolean contains( int value){
    		int i = 0;
    		while ( i < count && store[i] != value)
    			i++;
    		if ( i < count)
    			return true;
    		else
    			return false;
    	}
    	
    	// returns a string representation
    	public String toString(){
    		String x = "[ ";
    		for( int i = 0; i < count; i++)	
    			x = x+ ( store[i] + " ");	
    		return x + "]";
    	}
    	
    	//return the  value of given value
    	public int get(int index){
    		return store[index];
    	}
    	
    	// count the elements of array to the given value
    	public int countNumber(int value){
    		int a=0;
    		for(int i=0;i<count;i++)
    			{
    				if(store[i]==value)
    				a++;
    			}
    		return a;
    	}
    }
    
    //test class
    public class Intbaag 
    {
    
    	public static void main( String[] args) 
    	{
    		Scanner scan = new Scanner( System.in);
    
    		System.out.println( "Start of Intbaag" );        
    
    		// CONSTANTS
    
    		// VARIABLES
    
    		// PROGRAM CODE
    		Intbag bag= new Intbag(10);
    		
    		bag.addValue(1);
    		bag.addValue(1);
    		bag.addValue(5);
    		bag.addValue(7);
    		bag.addValue(9);
    		bag.addValue(1);
    		bag.addValue(6);
    		bag.addValue(1);
    		
    		System.out.println(bag);
    		if(bag.isContains(1))
    			bag.remove(1);
    		System.out.println(bag);
    	        
    	}
    
    }
    here is output:

    [ 1 1 5 7 9 1 6 1 ]
    [ 1 6 5 7 9 ]

    but I expect like this:
    [ 1 1 5 7 9 1 6 1 ]
    [ 5 7 9 6]
    Where is my fault i cant find it can you help me?
    Last edited by jacline; 03-20-2009 at 06:40 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    of course yes I tried but I cant debug it so I put it here.....

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    I implement a class called intBag that allows a variable sized collection of integer values to be stored.It has methods such as add,remove,contains.....I write a test class and create an intBag object then I add some values in it I want to remove number 1 but I can't.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    try iterating backwards in your remove method, not forward.

Similar Threads

  1. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 12:46 PM
  2. image removing
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-20-2008, 09:27 PM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 03:38 AM
  4. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 07:11 PM
  5. removing an object from memory
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 10:23 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
  •