Results 1 to 6 of 6
Thread: trouble in removing a value
- 03-20-2009, 02:34 PM #1
trouble in removing a value
here is my code
here is output: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); } }
[ 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 05:40 PM.
- 03-20-2009, 05:17 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try to debug the code and find the solution?
- 03-20-2009, 05:22 PM #3
of course yes I tried but I cant debug it so I put it here.....
- 03-20-2009, 05:31 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok then at least explain what you are going to do on this code?
- 03-20-2009, 05:39 PM #5
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.
-
Similar Threads
-
removing reference
By ajith_raj in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 11:46 AM -
image removing
By Triss in forum New To JavaReplies: 3Last Post: 01-20-2008, 08:27 PM -
Removing characters
By kDude in forum New To JavaReplies: 3Last Post: 12-03-2007, 02:38 AM -
removing duplicates from arrays
By bugger in forum New To JavaReplies: 3Last Post: 11-13-2007, 06:11 PM -
removing an object from memory
By ravian in forum New To JavaReplies: 1Last Post: 11-12-2007, 09:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks