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

3
  • Welcome to SO.Please read How to 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<String> which can be appended to. Commented Mar 26, 2021 at 6:05
  • thank you for your help. And how can I convert List<String> to add in info[] (because info[] is used in customList class that will show the data in multiple listview. Commented Mar 26, 2021 at 6: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. Commented Mar 26, 2021 at 6:33

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. I'm beginner and now I follow this 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[].
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
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
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.