-1

I'm trying to iterate over string arrays inside a ArrayList. How is this possible?

public ArrayList<String[]> arrayList = new ArrayList<String[]>();

// [ [12, "Something"], [6, "Thing"], [3, "Cookies"] ]

public void getText(final List arrayList) {
  for (String[] array : arrayList) {
    Log.d("Text", array[1]);
  }
}

Error (line 4)

  • Required: String[]
  • Found: Object
JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
Kenny
  • 1,110
  • 3
  • 13
  • 42

3 Answers3

1

You did not specified the actual type of data which your List into getText method is going to take. You need to replace final List arrayList with final List<String[]> arrayList into getText()

public void getText(final List<String[]> arrayList) {
  for (String[] array : arrayList) {
    Log.d("Text", array[1]);
  }
}

You should read What is a raw type and why shouldn't we use it?.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • While correct, code only answers are discouraged - and you should know that. Please add an explanation of what the problem was and how you have fixed it. – Boris the Spider Aug 25 '14 at 08:36
  • @PankajKumar Thank you so much! I'm gonna make this answer correct when the time expires. You are a hero! – Kenny Aug 25 '14 at 08:39
0

It looks like maybe you are trying to use an ArrayList when just a good old String Array by itself will do the trick. I certainly understand ArrayLists are great tools, but it seems unnecessary here without more context.

Just iterate over the String array as follows:

String[] array = yourInputSourceHere;

for( int i = 0 ; i < array.length; i++ ) {
     Log.d("Text", array[i]);
}
Dominic Holt
  • 162
  • 1
  • 12
0

To Make use of For each loop do the following

for (String array:arraylist)

{
// use Array as one String object
System.out.println(array);
}