简体   繁体   中英

Arrays and For-loop - Incorrect output while printing Random elements

I am pretty new in this world, and I must say sometimes things that looks easy are pretty harsh.

I am stuck with a task that entails dealing with an array and for -loops.

I should iterate over the array and for every iteration step print a different random string. My current code is not working correctly, the only thing I'm getting a random item and the same index printed multiple times.

My output right now:

relax
2
2
2
2

How can I fix that and get a correct randomized output?

My code:

public static void main(String[] args) {
    int i;
    
    String Cofee[] = {"pick it","drink it","relax","put it in a cup",};
     
    java.util.Random randomGenerator = new java.util.Random();

    int x = Cofee.length;
    int y = randomGenerator.nextInt(x);
    
    String frase = Cofee[y] ;
    System.out.println(frase);
    
    for(i = 0; i < Cofee.length; i++)
        System.out.println(y);      
}    

You assign a value to y once, and you print y repeatedly. The value of y doesn't change. To do that, you would need to call randomGenerator.nextInt(x) for each iteration of the loop !

However, if you want to randomize and print the array, use:

public static void main(String[] args)  
{
    String[] coffee = {"pick it","drink it","relax","put it in a cup",};
    // this wraps the array, 
    // so modifications to the list are also applied to the array
    List<String> coffeeList = Arrays.asList(coffee);
    Collections.shuffle(coffeeList);
    
    for(String value : coffee)
        System.out.println(value);      
}

As an aside, don't use String coffee[] , but use String[] coffee . Although Java allows putting the array type after the variable name, it is considered bad form.

Or use a list directly:

public static void main(String[] args)  
{
    List<String> coffeeList = Arrays.asList("pick it","drink it","relax","put it in a cup");
    Collections.shuffle(coffeeList);
    
    for(String value : coffeeList)
        System.out.println(value);      
}

For that, you can implement a shuffling algorithm.

It's not so scare as it might sound at first. One of the famous classic shuffling algorithms, Fisher–Yates shuffle is relatively easy to grasp.

The core idea: iterate over the given array from 0 to the very last index, and for each index swap the element that corresponds to the randomly generated index between 0 and the current index ( i ) with the element under the current index.

Also, I would advise creating a separate array representing indices and shuffle it in order to preserve the array of string its initial state (you can omit this part and change the code accordingly if you don't need this).

That's how it might be implemented:

public static final Random RANDOM = new Random(); // we need an instance for random to generate indices

A Fisher–Yates shuffle implementation:

public static void shuffle(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        int j = RANDOM.nextInt(i + 1); // generating index in range [0, i]
        swap(arr, i, j);               // swapping elements `i` and `j`
    }
}

Helper-method for swapping elements:

public static void swap(int[] arr, int i, int j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

Usage-example:

String[] coffee = {"pick it","drink it","relax","put it in a cup"};
        
int[] indices = new int[coffee.length];
for (int i = 0; i < indices.length; i++) indices[i] = i; // or Arrays.setAll(indices, i -> i); if you're compfortable with lambda expressions
        
shuffle(indices);
        
for (int i = 0; i < coffee.length; i++) {
    String next = coffee[indices[i]];
    System.out.println(next);
}

Output:

drink it
pick it
put it in a cup
relax

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