0

I have info[]

private String info[] = {};

while (dataSnapshotsChat.hasNext()) {
  DataSnapshot dataSnapshotChild = dataSnapshotsChat.next();
  String data1 = dataSnapshotChild.getValue(String.class);

and I want to append data1 to info[] anyone help

Thomas
  • 87,414
  • 12
  • 119
  • 157
nisawaras
  • 15
  • 3
  • Welcome to SO.Please read [ask] and note that Java is not like JavaScript (where I assume you've got the idea of appending to an array from). In Java an array has a fixed length and if you need to increase that length you need to create a new array and copy the elements over. What you want is a `List` which can be appended to. – Thomas Mar 26 '21 at 06:05
  • thank you for your help. And how can I convert List to add in info[] (because info[] is used in customList class that will show the data in multiple listview. – nisawaras Mar 26 '21 at 06:28
  • Well, `info[]` isn't a type in your case but just a name along with the bad practice (my opinion) of putting the type info _around_ it. Better would be `String[] info` so the type is `String[]`. That being said, there's the `List.toArray()` method which you might want to read up on. The call could be something like `info = yourList.toArray(new String[0]);` but note that this _replaces_ info as you can't _append_ to an array. – Thomas Mar 26 '21 at 06:33

2 Answers2

0

plenty of methods for copying to array in HERE - in fact you have to create new array with size+1, as usual arrays have fixed size. some example method:

static <T> T[] append(T[] arr, T element) {
    final int N = arr.length;
    arr = Arrays.copyOf(arr, N + 1);
    arr[N] = element;
    return arr;
}

your call

append(info, data1);

BUT I must aware you that this isn't well-perfomance way, it would be better to use ArrayList for info instead of not-so-efficient and fixed size array. copying whole array to new one with size N + 1 (and additional item) is in fact heavy operation and you want to use it in while method...

if you would have

private ArrayList<String> info = new ArrayList<>();

then you can simply call add inside while safely

info.add(data1);
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thank you very much. I'm beginner and now I follow this https://java2blog.com/android-custom-listview-with-images-text-example/ but in this web countryname[] is my info[] ,they put " " in that but in my app I want to put data1 in while to info[]. – nisawaras Mar 26 '21 at 07:11
  • I don't see in this tutorial any adding to array, all are fixed size. thats why these collections are arrays. "modifying" arrays content isn't efficient so I strongly recomend to use `ArrayList` – snachmsm Mar 26 '21 at 07:22
  • Thank you everyone I really appreciate receiving all your help but I tried so hard but I'm failed and I have a new big problem to solve when I can fix it I will reply immediately – nisawaras Mar 26 '21 at 15:03
0

You should probably follow what @Thomas is saying, here is a quick solution though.

String info[];
    
List<String> tempList = new ArrayList<>();

// Your code with modifications
while (dataSnapshotsChat.hasNext()) {
    DataSnapshot dataSnapshotChild = dataSnapshotsChat.next();
    String data1 = dataSnapshotChild.getValue(String.class);

    //add data1 to temp list
    tempList.add(str);

    // ...

}

// must have size
info= new String[tempList.size()];

for (int i = 0; i < tempList.size(); i++) {

    info[i] = tempList.get(i);

}

// Do what you want with info