-1

i was reading into this thread Removing an element from an Array (Java) And saw you could use ArrayUtils but i am unsure how?

This is the code so far

package javatesting;
import static java.lang.System.*;
public class main
}
public static int countIt( int[] iRay, int val )
{
 int count = 0;
 for(int item : iRay)
{
if( item == val )
{
 count = count + 1;
}
}
return count;
}

public static int[] removeIt( int[] iRay, int val )
{
 return null;
}

public static void printIt( int[] iRay  )
{
 for(int item : iRay)
 {
  out.print(item + " ");
 }
}

public static void main(String[] args)
{
 int[] nums = {7,7,1,7,8,7,4,3,7, 9,8};

 printIt( nums );
 System.out.println("\ncount of 7s == " + countIt( nums, 7 ));
 nums = removeIt( nums, 7 );
 printIt( nums );
 System.out.println("\ncount of 7s == " + countIt( nums, 7 ));
}

I tryed placing it in removeIt but i dont understand how it should connect? My AP Teacher didnt explain it to us If possible could one of you link me a tutorial for java As i understand it asks for the count of non sevens that why i want ot create a array with the seven's removed using the ArrayUtils (i am using eclipse if it matters)

Community
  • 1
  • 1
Will309
  • 15
  • 1
  • 8

2 Answers2

0

The usage of ArrayUtils.removeElement is pretty straight forward and would look like this:

public static int[] removeIt( int[] iRay, int val )
{
    return ArrayUtils.removeElement(iRay, val);
}

Also, avoid asking things like "If possible could one of you link me a tutorial for java." The StackOverflow community wont respond to generic request like this if it can easily be Googled.

Proper usage can be found here.

Foggzie
  • 9,691
  • 1
  • 31
  • 48
0

You can use

ArrayUtils.removeElements(array, element)

However, since you this is for a class, your professor probably doesn't want you to be using any libraries. In which case you would create a new array, loop through the old one, extract all entries whose value is not 7, add them to the new array and return it.

If you wanted, you could also do a while loop of

while(ArrayUtils.indexOf(array, 7) =! -1){
   ArrayUtils.removeElement(array, 7)
}

However, please make sure you are allowed/encouraged to use libraries. And if you are, make sure you download the ArrayUtils.jar and include it in your build path, otherwise you will not be able to use its static methods.

Schaka
  • 772
  • 9
  • 20