-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

1
  • 2
    What part of the error message don't you understand? Everything you need to know is stated there. Commented Mar 23, 2014 at 0:17

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, an Integer is unboxed to an int if you're using generics
@hd1 Yes, but an Integer[] is not. Arrays are not subject to boxing/unboxing.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.