简体   繁体   中英

Entering a string into a random position of an array using Java

I'm trying to write a program that has an array of ten names. I asked for input of a name using a scanner, and I want the ten names to be assigned a random position of the array.

This is my code so far but I'm pretty much stuck.

import java.util.Scanner

class RandomArray {

    public static void main(String[] args) {

        String [] NamesArray = new String[10]
        Scanner sc = new Scanner(System.in){
        System.out.println("Input first name: ");

    }
}

I'm a complete beginner. Thanks

You might try using a list instead of an array to store the names. This will allow you to add them in order and shuffle them later.

First, you want to create an ArrayList to store the names:

ArrayList<String> list=new ArrayList<String>();

Then get the names. I would recommend a loop to save a lot of typing. Then, as each name is entered, add it to the list (replace "name" with the name you are going to add):

list.add(name);

Then you can shuffle the list:

Collections.shuffle(list);

Then loop through the list and print them (or whatever you want to do with them). You could also turn the list into an array:

list.toArray(new String[list.size()]);

There are two overall strategies you might use.

  1. Put the names into the array in the order they're entered, and then shuffle them afterwards.
  2. Start by generating an array of integers from 0 to 9, and shuffle these integers; then use this array to determine where to place each name as it comes in. (Remember that arrays are indexed from 0.)

I'd go with the first: it seems a little easier.

Details on how to shuffle an array efficiently can be found in this question .

If you don't need it to be an array, by the way, you can use an ArrayList<String> , which would then allow you to use a standard Java method to shuffle it: the Collections.shuffle() method.

You could use class Collections it has a shuffle method:

String [] namesArray = new String[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < namesArray.length; i++) {
    System.out.print("Input name: ");
    namesArray[i] = sc.next();
}
System.out.println("Input: " + Arrays.toString(namesArray));
Collections.shuffle(Arrays.asList(namesArray));
System.out.println("Input shuffled (random): " + Arrays.toString(namesArray));

With the help of chiastic-security's comment, I wrote the code for you. As a beginner I would find it hard to understand otherwise.

// Method 1. Put the names into the array in the order they're entered, and then shuffle them afterwards.

String[] NamesArray = new String[10];
Scanner sc = new Scanner(System.in);

for (int i = 0; i < NamesArray.length; i++) {
    System.out.print("Enter name No. " + (i + 1) + ": ");

    String name = sc.next();
    NamesArray[i] = name;
}
swapShuffle(NamesArray);

for (String name : NamesArray) {
    System.out.println(name);
}

or alternatively,

// Method 2. Start by generating an array of integers from 0 to 9, and shuffle these integers; then use this array to determine where to place each name as it comes in.
Integer[] nameOrder = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
swapShuffle(nameOrder);

String[] NamesArray = new String[10];
Scanner sc = new Scanner(System.in);

for (int i = 0; i < NamesArray.length; i++) {
    System.out.print("Enter name No. " + (i + 1) + ": ");

    String name = sc.next();
    NamesArray[nameOrder[i]] = name;
}

where both methods are using this static method for the shuffling:

public static void swapShuffle(Object[] objects) {
    Random r = new Random();

    for (int i = 0; i < objects.length; i++) {
        int n = r.nextInt(objects.length);

        Object o = objects[i];
        objects[i] = objects[n];
        objects[n] = o;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM