-1
import java.util.*;
public class Tester2
{
  public static void main(String[] args)
  {
    int[] array = new int[] {1,2,3,1,2,3};

    System.out.println(Arrays.toString(deleteElement(array, 1)));

  }

  public static int[] deleteElement(int [] array, int target)
  {
    ArrayList<Integer> a1 = new ArrayList<Integer>();

    for(int i=0; i<array.length; i++)
    {
      if (array[i] != target)
    a1.add(array[i]);
    }

    int[] returnedArray = new int[a1.size()];
    returnedArray = a1.toArray(returnedArray);

    return returnedArray;

  }  
}

When I try to compile this code I get the following error:

1 error found:

File: /Users/Hyeunjoon/Desktop/CS/A4/Tester2.java [line: 25]

Error: /Users/Hyeunjoon/Desktop/CS/A4/Tester2.java:25: cannot find symbol

symbol : method toArray(int[])

location: class java.util.ArrayList

Can anyone help me out? I don't understand why I am getting this error

Filburt
  • 16,221
  • 12
  • 59
  • 107
user3451078
  • 33
  • 1
  • 3

1 Answers1

0

Ignoring other possible improvements, you have:

returnedArray = a1.toArray(returnedArray);

If you look at the documentation for ArrayList<T>.toArray(T[]), you'll see that it takes an array of T. In your code, T is Integer. An int[] is not an Integer[].

You'd have to temporarily store the results in an Integer[] then convert one by one into an int[] (in which case you might as well avoid the Integer[] middle-man entirely).

Jason C
  • 34,234
  • 12
  • 103
  • 151
  • Actually, an [Integer is unboxed to an int](http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html) if you're using [generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html) – hd1 Mar 23 '14 at 00:27
  • 1
    @hd1 Yes, but an `Integer[]` is not. Arrays are not subject to boxing/unboxing. – Jason C Mar 23 '14 at 00:27
  • 1
    Thanks that makes sense I am very new to java and programming in general. I failed to differentiate between primitives and reference types. I changed the original array to Integer[] instead of int[] and the method worked fine, thanks Jason. – user3451078 Mar 23 '14 at 00:39