0

I am creating my own set class . But At the beginning , I take a warning. I use in this link How to create a generic array?. But I have already take warning.

This is my warning message:

MySet.java:11: warning: [unchecked] unchecked cast
        data = (T[]) new Object[10];
                     ^
  required: T[]
  found:    Object[]
  where T is a type-variable:
    T extends Object declared in class MySet
1 warning

This is my beginner code:

Main.java

public class Main{

    public static void main(String[] args){

        MySet<Integer> a = new MySet<Integer>();

        System.out.printf("empty or not = %d\n",a.empty());

    }

}

MySetInterface.java

public interface MySetInterface<T> {
    public int empty();
}

MySet.java

public class MySet<T> implements MySetInterface<T>{

private T[] data;
private int used;
private int capacity;

public MySet(){

    used = 0;
    capacity = 1024;
    data = (T[]) new Object[10];
}

public int empty(){

    if(used == 0){
        return 1;
    }
    else{
        return 0;
    }

}

If I use

@SuppressWarnings("unchecked")
        data = (T[]) new Object[10];

I take this error message now:

MySet.java:12: error: <identifier> expected
        data = (T[]) new Object[10];
            ^
1 error
5
  • 1
    Maybe a Collection helps in your case, e.g. an ArrayList? Commented Dec 20, 2017 at 10:22
  • Maybe you could try public class MySet<T extends Object>? Commented Dec 20, 2017 at 10:25
  • @C-Otto It is my homework and I can not any Collection Commented Dec 20, 2017 at 10:41
  • @HazeErasmo I take same warning . Commented Dec 20, 2017 at 10:44
  • The @SuppressWarnings annotation syntactically needs to attach to a definition rather than to an assignment statement. So if you define a variable you can put @SuppressWarnings on it, or you can put it on an enclosing method or class. Commented Dec 20, 2017 at 11:36

1 Answer 1

1

If you read the answer to the question that you provided it gives a very thorough explanation as to why the warning is shown and it also provided a valid workaround.

The above code have the same implications as explained above. If you notice, the compiler would be giving you an Unchecked Cast Warning there, as you are typecasting to an array of unknown component type. That means, the cast may fail at runtime. For e.g, if you have that code in the above method:

Suggested typesafe code.

public <E> E[] getArray(Class<E> clazz, int size) {
    @SuppressWarnings("unchecked")
    E[] arr = (E[]) Array.newInstance(clazz, size);

    return arr;
}

Explanation is provided in the answer

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

5 Comments

@PillHead MySet(Class<T> clazz) { ... }.
@PillHead In your posted code, I don't want to use E[ ] in E[ ] arr, because arr already declare in data field. So if I use this code, I do shadowing to my data field.So what can I do?
to be honest the simplest thing to do is add @SuppressWarnings("unchecked") above this line data = (T[]) new Object[10];
@but now I take another error message.Could you look my edited post .
try moving the annotation to above the constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.