2

On the material page I found the following example for AutoCompleteTextView:

int layoutItemId = android.R.layout.simple_dropdown_item_1line;
String[] dogArr = getResources().getStringArray(R.array.dogs_list);
List<String> dogList = Arrays.asList(dogsArr);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, layoutItemId, dogList);

AutoCompleteTextView autocompleteView =
(AutoCompleteTextView) findViewById(R.id.autocompleteView);
autocompleteView.setAdapter(adapter);

Source: https://materialdoc.com/components/autocomplete/

What is the point of this part:

List<String> dogList = Arrays.asList(dogsArr);

Why turning it into an ArrayList when the AutoCompleteTextView also takes a String array?

Florian Walther
  • 4,170
  • 2
  • 24
  • 63

3 Answers3

0

When you know only going to work with a fixed number of elements, you should Array. If not, use Lists.

My personal opinion is use list. Lists makes code very inflexible and easy to use.

Tung Tran
  • 2,229
  • 2
  • 12
  • 22
0
  • You can initialize Java arrays at compile time, like:

String data[] = { "a", "b", "c" };

  • In old versions of Java there was also the case for type safety. ArrayList elements had to be casted to the original type whereas Java arrays where type safe.
  • Java arrays are part of the language and you will not be able to change them. ArrayList is part of the Java API. If you need (I do not recommend it though) you could substitute your own library to implement the ArrayList job

Check This Question For More Information

See This question

Rj_Innocent_Coder
  • 968
  • 1
  • 8
  • 24
0

If you have an array, it has to have a fixed size. Dynamically adding and removing the elements are difficult to manage and you have to have new array created every time you add a new item. Similarly for removing item.

With ArrayList it is easy to manage as it doesn't get created with a static size. Thus at runtime you can easily add and remove elements.

ArrayList is the ideal datastructure to use here.