error: Array required, but String found

前端 未结 2 2348
闹比i
闹比i 2021-01-29 03:39

I\'ve declared public static arrays for name and id:

public static String[] name = new String[19];     
public static int[] id  = new int[19];

相关标签:
2条回答
  • 2021-01-29 04:08

    You have a collision between the static name String array and the local name String variable passed to the add method.

    The best solution would be to use different names. It would make the code much easier to understand.

    If you still insist on using the same name, you can resolve the name collision by accessing the static array using the class name:

    YourClassName.name[i]= name;
    

    The same applies to your id int array and id int variable.

    0 讨论(0)
  • 2021-01-29 04:09

    Pay close attention to how you're using your variables. name (inside of your method) is a String, but you're doing an array element access on it. with i. The same is true for id; it is an int, but you're doing an array element access on it.

    You're effectively shadowing your static variables, which causes confusion and heartache.

    Consider renaming the parameters to your method, or using the class name to reference them.

    Either:

    public static boolean add (String theName , int theIds, int i)
    

    or:

    // for every usage of id and name as arrays
    MyClass.name[i]= name;
    MyClass.id[i]=id;
    
    0 讨论(0)
提交回复
热议问题