0

fellow programmers: So I know how to fill a String[] Array with fixed entries like this:

String[] restaurants = new String[] { "McDonalds", "KFC", "Tacos", "Something", "Huh", "WTF"};

But thing is: -I need to read data from a File or a Database -I need a String[] for a ListAdapter -I cannot fix the size of the Array, because I don't know the amount of data i'm going to receive

I have been searching in the web about this, but i'm unable to find the answer. Can anyone point me in the right direction? Thanks in advance :D

0

1 Answer 1

1

If you don't know how many entries you will need to read, read them into a List and then convert the list to an array when you are done; e.g.

    List<String> l = ...
    // read list
    String[] restaurants = l.toArray(new String[l.size()]);
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for being a noob but when I add List<String> list; it needs to be initialized, how do I do that?
@CodeKrieger: List of implementing classes (e.g. l = new ArrayList<String>())
@CodeKrieger: if you go through the link in my answer there is a very similar example of what you are trying to achieve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.