预期但我不想使用标识符

时间:2022-12-28 07:08:17

This is my error message:

这是我的错误消息:

Set.java:12: error: <identifier> expected
        data = (T[]) new Object[10];
            ^
1 error

This is my code of Set.java.

这是我的Set.java代码。

public class Set<T>{

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

            public Set(){

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

            public int empty(){

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

            }

If I do T[ ] data = (T[]) new Object[10]; the error gone. But I don't want use T[ ] because , I have done this already at data field which is private T[] data; So I want to use data field with data = (T[]) new Object[10]; not a new T[ ] data.But I take this error message. What can I do?

如果我做T [] data =(T [])new Object [10];错误消失了。但是我不想使用T [],因为我已经在数据字段中完成了这个私有T []数据;所以我想用data =(T [])new Object [10];不是新的T []数据。但我收到此错误消息。我能做什么?

2 个解决方案

#1


1  

Just move the annotation and the initialization to the declaration. It doesn't depend on anything in the constructor. Same goes for the other two variables actually. Then you can remove the constructor. Don't write code you don't have to write.

只需将注释和初始化移动到声明即可。它不依赖于构造函数中的任何内容。实际上其他两个变量也是如此。然后你可以删除构造函数。不要编写您不必编写的代码。

public class Set<T> {
    @SuppressWarnings("unchecked")
    private T[] data = (T[]) new Object[10];
    private int used = 0;
    private int capacity = 1024;

    //constructor removed

    public int empty(){
        //...

#2


0  

You cannot use annotations within a method body. If you want to ignore the warning, move the annotation to before the method declaration:

您不能在方法体中使用注释。如果要忽略警告,请将注释移到方法声明之前:

@SuppressWarnings("unchecked")
public Set()
{
    used = 0;
    capacity = 1024;
    data = (T[]) new Object[10];
}

#1


1  

Just move the annotation and the initialization to the declaration. It doesn't depend on anything in the constructor. Same goes for the other two variables actually. Then you can remove the constructor. Don't write code you don't have to write.

只需将注释和初始化移动到声明即可。它不依赖于构造函数中的任何内容。实际上其他两个变量也是如此。然后你可以删除构造函数。不要编写您不必编写的代码。

public class Set<T> {
    @SuppressWarnings("unchecked")
    private T[] data = (T[]) new Object[10];
    private int used = 0;
    private int capacity = 1024;

    //constructor removed

    public int empty(){
        //...

#2


0  

You cannot use annotations within a method body. If you want to ignore the warning, move the annotation to before the method declaration:

您不能在方法体中使用注释。如果要忽略警告,请将注释移到方法声明之前:

@SuppressWarnings("unchecked")
public Set()
{
    used = 0;
    capacity = 1024;
    data = (T[]) new Object[10];
}