0

What is the fundamental difference between declaring

String foo[];

and

String[] foo;

Ex: I am using a spinner and the first example works while the second example doesn't, even though I don't have any other problems in the rest of my code using the second declaration method.

@Override
public void onClick(View view) {
    String[] Condicoes = {fase, comunicacao,tipo_comunicacao};
    String concat = "";
    for (int k = 0; k < Condicoes.length; k++) {
        String newfer[] = StringMapper.get(Condicoes[k]);

        for(int i = 0; i<newfer.length; i++) {
            //ArrayAdapter<String> spinningspinenER = new ArrayAdapter<String>(InsertID.this, android.R.layout.simple_spinner_item, newfer);

            concat += newfer[i];
            concat += "\n";
        }
    }
    textView.setText(concat);
}

This is the code that gives me an error if I use:

String[] newfer = StringMapper.get(Condicoes[k]);

in the commented part

   ArrayAdapter<String> spinningspinenER = new ArrayAdapter<String>(InsertID.this, android.R.layout.simple_spinner_item, newfer);

it says that it can not build the ArrayAdapter

hisener
  • 1,431
  • 1
  • 17
  • 23
Nuno Mendes
  • 113
  • 1
  • 11
  • add some exaple of what you do, what you exepect and what it's huppen. – Stefano R. Aug 28 '17 at 12:45
  • There is no difference. It is the same see : https://stackoverflow.com//questions/1200621/how-do-i-declare-and-initialize-an-array-in-java – Jens Aug 28 '17 at 12:45
  • what problem you get? – Youcef LAIDANI Aug 28 '17 at 12:45
  • the `[]` placement is not possible to be the root cause of the problem in the program, both declarations are fully legal in java code. Paste the stack trace you get on failure. – DevDio Aug 28 '17 at 12:47
  • Well it seems my problem disappeared, it might have been a problem declaring the array at first. Because when i switched it around again it was fine, maybe a missing colon or something. Thanks to everyone anyway – Nuno Mendes Aug 28 '17 at 12:50

1 Answers1

1

Both are basically the same

String[] foo; //preferred way  

or String foo[];// works but not preferred way

String arrays are put in the following way

 String[] Condicoes = {"fase", "comunicacao","tipo_comunicacao"};

for array adapter I think this the one you are trying to use

https://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, int, T[])

hisener
  • 1,431
  • 1
  • 17
  • 23
Mitesh
  • 1,544
  • 3
  • 13
  • 26