0

I am making an ArrayList of objects that will be displayed in a ListView. When I use Object[] ObjectList = new Object[length] it expects an exact length for the entire array upon creation. I have tried making the value of the length an int variable but it appears it doesn't update the length when the variable is changed. How can I accomplish this? I am kinda new to Java, so thanks to anyone who helps out!

Kyle James
  • 13
  • 1
  • `Object[length]` doesn't create an ArrayList, it creates an array. Use `new ArrayList` to create an ArrayList of Objects. – Carcigenicate Aug 15 '17 at 19:26
  • Possible duplicate of [https://stackoverflow.com/questions/157944/create-arraylist-from-array?rq=1](Create ArrayList from array). Though I'm a bit unsure... – Randall Arms Aug 15 '17 at 19:34

3 Answers3

3

Use List interface of Collections framework. E.g. List<String> data = new ArrayList<>(). You can conver it to array if you need with data.toArray() method

Ivan Pronin
  • 1,768
  • 16
  • 14
  • how can I specify the object type of the array when using the toArray() method? – Kyle James Aug 15 '17 at 19:40
  • The type of the array will be inferred from the type of list items. E.g. List will produce a `String[]` array – Ivan Pronin Aug 15 '17 at 19:43
  • I have an ArrayList of a custom object (Obj) using List list = new ArrayList<>; and when I have the Array, created by Obj[] tempList = list.toArray; It claimed that it needs Obj[] but found Object[]. – Kyle James Aug 15 '17 at 19:56
2

Arrays have a fixed size. Once an array is created, the size can't be changed any more. You can only create a new array and change the reference your variable is holding!

In other words - assume you start with:

Object[] items = new Object[5];

Later you figure: I need more space:

Object[] moreItems = new Object[15];

And then you can use System.arraycopy() to copy the content of the first array into the second.

For the record:

  • List/ArrayList are not Arrays.
  • ArrayList uses the approch described above under the covers to implement that "dynamically" growing array experience for you
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

I will not repeat what others have very well said.

About your actual problem, android.widget.ListView is designed to be populated with array type.
Now if data used to populate the ListView have a size variable and may often changed, you have an alternative : using an ArrayList wrapped in an ArrayAdapter.

During the onCreate() method you could create the adapter and set it to the ListView :

List<String> list = new ArrayList<>();
...
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                 this, 
                 android.R.layout.listview,
                 arrayList);
listView.setAdapter(arrayAdapter); 

Now, you don't have any longer the problem of the fixed size array as you use an ArrayList that doesn't have this limitation.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 1
    Yep, ArrayList and ArrayAdapter is the answer. Take a look at https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView – Unknown Aug 15 '17 at 22:39