Search code examples
javaarraysarraylistshuffleparallel-arrays

How do I get shuffled arrays in a list to match up with each other in Java?


I am new to coding and I wanted to know how I can code this program so that the arrays are matched. (apples and the number 1 match, bananas and the number 2 match and mangos and the number 3 match) I've used assert fruits.size() == numbers.size(); but its still not working, is there a way I can fix that?

package example;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.JOptionPane;

public class Example {
    
    public static void main(String[] args) {
final StringBuilder generator = new StringBuilder();
final List<String> fruits = Arrays.asList("apples", "bananas", "mangos");
final List<Integer> numbers = Arrays.asList(1, 2, 3);

Collections.shuffle(fruits);
Collections.shuffle(numbers);

assert fruits.size() == numbers.size();

for (int i = 0; i < fruits.size(); i++) {
int a = i+1;
    final String name = JOptionPane.showInputDialog(null, "Enter person " + a + "'s name");

    generator.append(name)
        .append(" likes ")
        .append(fruits.get(i))
        .append(" and the number ")
        .append(numbers.get(i))
        .append("\n");
}

JOptionPane.showMessageDialog(null, generator);
    }
}

I would like the output to be:

Alex likes mangos and the number 3

John likes apples and the number 1

Jane likes bananas and the number 2

instead of:

Alex likes mangos and the number 1

John likes bananas and the number 3

Jane likes apples and the number 2


Solution

  • I'd say the fruit and the number conceptually belong to the same object (a pair made of number + fruit name).

    You should make an array of those pairs (for example using AbstractMap.SimpleEntry<String, Integer>, which is basically a pair with the fruit name as a key and the number as a value) and shuffle that.

    Then you can retrieve the name with getKey() and the number with getValue()

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.AbstractMap;
    import javax.swing.JOptionPane;
    
    public class Example {
    
        public static void main(String[] args) {
            final StringBuilder generator = new StringBuilder();
    
            final List<AbstractMap.SimpleEntry<String, Integer>> fruits = Arrays.asList(
                    new AbstractMap.SimpleEntry("apples", 1),
                    new AbstractMap.SimpleEntry("bananas", 2),
                    new AbstractMap.SimpleEntry("mangos", 3)
            );
    
            Collections.shuffle(fruits);
    
            for (int i = 0; i < fruits.size(); i++) {
                final String name = JOptionPane.showInputDialog(null, "Enter person " + (i + 1) + "'s name");
    
                generator.append(name)
                        .append(" likes ")
                        .append(fruits.get(i).getKey())
                        .append(" and the number ")
                        .append(fruits.get(i).getValue())
                        .append("\n");
            }
    
            JOptionPane.showMessageDialog(null, generator);
        }
    }
    

    Example output:

    enter image description here