Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Arrays: question about "removing" items.

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays: question about "removing" items.

    hello.. guys
    i am trying to figure out how to remove "some" x from array..

    i had this so far..

    but still i get errors :S with..

    				array[i]=arr[i];

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    	at Even.remove(Even.java:150)
    	at Even.main(Even.java:127)

    	public static int[] remove(int[] arr,int x) {
     
    		int count=0;
     
    		for (int i=0;i<arr.length;i++)
    			if (arr[i] == x)
    				count++;
     
    		int[] array = new int[arr.length-count];
     
    		for (int i=0;i<arr.length;i++)
    			if (arr[i] != x)
    				array[i]=arr[i];
     
    		return array;
     
     
    	}


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Arrays: question about "removing" items.

    You're copying over values into the same indices between the two arrays, leaving blank spots in array.

    count = 0;
    for (int i = 0; i < arr.length; i++)
    {
         if (arr[i]!=x)
         {
              array[count] = arr[i];
              count++;
         }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrays: question about "removing" items.

    good.. i forgot that
    thanks alot

    	public static int[] remove(int[] arr,int x) {
     
    		int count=0,k=0;
     
    		for (int i=0;i<arr.length;i++)
    			if (arr[i] == x)
    				count++;
     
    		int[] array = new int[arr.length-count];
     
    		for (int i=0;i<arr.length;i++)
    			if (arr[i] != x)
    			{
    				array[k]=arr[i];
    				k++;
    			}
     
    		return array;
     
     
    	}

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  3. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  4. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM