UltraInstinctMessi
UltraInstinctMessi

Reputation: 57

Converting array list to a string array

I've been trying to convert my string array list to a string array so I can print it but have been unable to do so.

This is the class I have, randomQuestion which takes in an array list from the gameQuestions method in the same class.

I have never tried to convert an array list using a loop before hence the difficulty, I was able to convert it fine with the code

String[] questions = data1.toArray(new String[]{});

But I need it to loop through using a for loop to store it in an array which I can then print one at a time once a question is answered successfully.

The error I'm receiving from netbeans is cannot find symbol Symbol:methodtoArray(String[]) for the .toArray portion below.

public String[] randomQuestion(ArrayList data1) {
    Collections.shuffle(data1);
    for (int question = 0; question < 10; question++) {
        ranquestions = data1.get(question).toArray(new String[10]);
    }
    return ranquestions;
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 102

Answers (4)

张建明
张建明

Reputation: 11

You can use List.toArray(). Class List has a method:

<T> T[] toArray(T[] a);

Upvotes: 1

Stephane Piedjou
Stephane Piedjou

Reputation: 149

You can do:

private String[] randomQuestions(ArrayList data){
    Collections.shuffle(data);
    return (String[]) data.toArray();
}

If you are sure you are getting a list of string (question) you can instead

private String[] randomQuestions(List<String> data){
    Collections.shuffle(data);
    return (String[]) data.toArray();
}

Edit 1

private static String[] randomQuestions(ArrayList data){
    Collections.shuffle(data);
    String[] randomQuestions = new String[data.size()];
    for(int i=0; i<data.size(); i++){
        randomQuestions[i] = String.valueOf(data.get(i));
    }
    return randomQuestions;
}

Upvotes: 0

user16320675
user16320675

Reputation: 135

The question is how to create an array with only 10 elements of the list, if I understood correctly.

Streams (Java 8):

String[] ranquestions = data1.stream()
                             .limit(10)
                             .toArray(String[]::new);

Loop (based on question, avoiding unnecessary changes):

String[] ranquestions = new String[10];
for(int question = 0; question < 10; question++) {
    ranquestions[question] = data1.get(question);
}

always assuming List<String> data1, if not some conversion is needed.
Example:

String[] ranquestions = data1.stream()
                             .limit(10)
                             .map(String::valueOf)
                             .toArray(String[]::new);

or, loop case:

    ranquestions[question] = String.valueOf(data1.get(question));

Upvotes: 0

deHaar
deHaar

Reputation: 18558

Assuming you have an ArrayList<String>, you can use String.join(delimiter, wordList) in order to concatenate all the elements to a single String:

public static void main(String[] args) {
    // example list
    List<String> words = new ArrayList<String>();
    words.add("You");
    words.add("can");
    words.add("concatenate");
    words.add("these");
    words.add("Strings");
    words.add("in");
    words.add("one");
    words.add("line");
    // concatenate the elements delimited by a whitespace
    String sentence = String.join(" ", words);
    // print the result
    System.out.println(sentence);
}

The result of this example is

You can concatenate these Strings in one line

So using your list, String.join(" ", data1) would create a String with the elements of data1 delimited by a whitespace.

Upvotes: 0

Related Questions