0

Beginner programmer here, I currently have a program written up that takes an array and a variable and compares the two, if the variable declared is within the array, the array will print out all the numbers within the array besides the number contained in the variable. This is working and functioning properly. Furthermore, what I would like to do now is take the code I've written up and put it in a functioning method for reuse but I am currently kind of stuck on doing so. The end result would be having the a new smaller array displayed that does not contain the number 17 anymore. Here is my code example.

public class arrayTest
{
   public static void main(String [] args)
   {
      int[]arrayA = {2,4,8,19,32,17,17,18,25,17};

      int varB = 17;

      for(int B = 0; B < arrayA.length; B++)
      {
         if(arrayA[B] != varB)
         {
            System.out.print(arrayA[B] + " ");
         }

      }
   }

      public static int newSmallerArray( int[] A, int b )
      {

      }
}
Clark1776
  • 1
  • 2
  • why do you have a return type of int? shouldn't it just print it like you've done within the main method? in other words a return type of void would be suitable or returning an array. – Ousmane D. Apr 05 '17 at 00:37
  • 1
    Do you want to return the smaller array, or only print it? Also your IDE has a refactor option to "extract method", probably – OneCricketeer Apr 05 '17 at 00:41
  • please refer this post [link] (http://stackoverflow.com/questions/5098540/delete-element-from-array) Its same as your question. – Rajith Pemabandu Apr 05 '17 at 00:51
  • Clark do any of the answers address your question? – Scary Wombat Apr 05 '17 at 01:20

3 Answers3

2

Ok, at this point

  int[]arrayA = {2,4,8,19,32,17,17,18,25,17};
  int varB = 17;

you can call

 newSmallerArray(arrayA, varB);

and then, in this method you would have the same looping code, but the variables names have changed

  for(int B = 0; B < A.length; B++)
  {
     if(A[B] != b)
     {
        System.out.print(A[B] + " ");
     }
  }

now the trouble is, you have declared this method as returning an int but there is nothing to return so maybe make it return void

public static void newSmallerArray( int[] A, int b )

next, I would also stick to java naming conventions

Full code sample returning a subset of the input array

using better names

public static void main(String[] args) throws SocketException {

    int[]arrayA = {2,4,8,19,32,17,17,18,25,17};
    int varB = 17;

    int [] na = newSmallerArray(arrayA, varB);
    // test
    System.out.println();
    for(int b = 0; na != null && b < na.length; b++)
    {
        System.out.print(na[b] + " ");
    }        
}

private static int[] newSmallerArray(int[] a, int b) {

    int count = 0;
    for(int x = 0; x < a.length; x++)
    {
        if(a[x] != b)
        {
            System.out.print(a[x] + " ");
            count++;
        }
    }

    if (count > 0) {
        int[] newArray = new int [count];
        count = 0;
        for(int x = 0; x < a.length; x++)
        {
            if(a[x] != b)
            {
                newArray[count++] = a[x];
            }
        }           

        return newArray;
    }

    return null;

}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

I would like it to return the array without 17 in the array anymore

Then, assuming you are using Java 8+, use an IntStream and filter out b values. Like,

public static int[] newSmallerArray(int[] arr, int b) {
    return IntStream.of(arr).filter(x -> x != b).toArray();
}

If you want to do it without Java 8 features, you could count the occurrences of the value to exclude. Like,

private static int countValue(int[] arr, int b) {
    int count = 0;
    for (int value : arr) {
        if (value == b) {
            count++;
        }
    }
    return count;
}

And use that to correctly size a new array, and then copy the values. Like,

public static int[] newSmallerArray(int[] arr, int b) {
    int[] ret = new int[arr.length - countValue(arr, b)];
    for (int i = 0, p = 0; i < arr.length; i++) {
        if (arr[i] != b) {
            ret[p++] = arr[i];
        }
    }
    return ret;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Just refactor your code that contains the main logic and move it to method. It would be easy to do with an IDE. Else, you have to copy the code to a new method and pass necessary arguments.

public class arrayTest
{
   public static void main(String [] args)
   {
      int[] arrayA = {2,4,8,19,32,17,17,18,25,17};
      int varB = 17;
      // Call with the array and variable you need to find.
      newSmallerArray(arrayA, varB);
   }

   public static void newSmallerArray( int[] arrayA, int varB)
   {
      for(int B = 0; B < arrayA.length; B++)
      {
         if(arrayA[B] != varB)
         {
            System.out.print(arrayA[B] + " ");
         }
      }
   }
}
prasanth
  • 3,502
  • 4
  • 28
  • 42