0

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 2
    Are you really using a [raw type as a parameter](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) (`ArrayList data1`)? I would recommend not doing that and instead specify what type of Objects your ArrayList contains via generics, that will make anything that follows with conversion etc. a lot easier. – OH GOD SPIDERS Aug 31 '21 at 12:39
  • 1
    The Problem is that we currently don't even know what data types your arraylist contain, so we cannot tell you how to convert those to anything. – OH GOD SPIDERS Aug 31 '21 at 12:40
  • It's a string array list which is used to scan in text from a text file (questions for the quiz). – UltraInstinctMessi Aug 31 '21 at 12:41
  • 1
    Exactly, if it was an `ArrayList`, you could `String.join` it to a single `String` using a delimiter of your choice. – deHaar Aug 31 '21 at 12:41

4 Answers4

1

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

<T> T[] toArray(T[] a);
Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
张建明
  • 11
  • 2
0

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • It is an array list string. I can scan in the questions from the text file. What I'm trying to do after scanning them in is to randomize the order of each of the scanned lines with collections.shuffle. Collections.shuffle is one solution I found to randomize the order of the questions I have which have been scanned into the array list. – UltraInstinctMessi Aug 31 '21 at 12:51
  • Sorry for the confusion. It's meant to print the randomized questions once it's stored in an array but "ranquestions = data1.get(question).toArray(new String[10]);" is not allowing me to store the questions in the string array. – UltraInstinctMessi Aug 31 '21 at 12:54
  • OK, that's something one could not have guessed from your question, sorry... – deHaar Aug 31 '21 at 12:57
  • I think I might just leave shuffling the array list to randomize. Is there another way that I could use to randomize an array and generate unique array (question) that hasn't been asked before? – UltraInstinctMessi Aug 31 '21 at 13:12
  • That brings another aspect into play... If you want to make sure not to create the same array more than one time, you will have to store each one you generate and compare it to a newly generated one. Discard and regenerate if it has been created before, otherwise use and store it. – deHaar Aug 31 '21 at 13:39
0

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));
user16320675
  • 135
  • 1
  • 3
  • 9
0

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;
}
  • I think I might just leave shuffling the array list to randomize. Is there another way that I could use to randomize an array and generate unique array (question) that hasn't been asked before? – UltraInstinctMessi Aug 31 '21 at 13:12