[Java] change seat with java

1 minute read

Change seats for 12 men and 8 women with Java

What you want to achieve

・Arrangement is random ・I want to place more than one woman somewhere in a row ・If people with bad eyes come in, fix it in front ・Easy code

result

Get from the shuffled list with get(). Made it look like a seat in System ~ println

・Arrangement is random → The list changes every time with Collections.shuffle() ・I want to place more than one woman somewhere in a row → In advance, I decided where to place women or men ・Fixing people with bad eyesight →Remove it from the list and delete “~get()” and set it as the name of the person to fix.

*When you run the program, it ends in a moment, so it’s a little boring *I can not avoid people who have been next to each other

code


import java.util.ArrayList;
import java.util.Collections;

public class Sekigae4 {
        public static void main(String[] args) {

                ArrayList<String> man = new ArrayList<String>();
                man.add("Men 1");
                man.add("Men 2");
                man.add("Men 3");
                man.add ("Male 4");
                man.add("Men 5");
                man.add("Men 6");
                man.add("Male 7");
                man.add("Male 8");
                man.add("Male 9");
                man.add("Men 10");
                man.add("Men 11");
                man.add("Male 12");

                ArrayList<String> woman = new ArrayList<String>();
                woman.add("woman1");
                woman.add("woman2");
                woman.add("woman3");
                woman.add("woman4");
                woman.add("woman5");
                woman.add("woman6");
                woman.add("Woman7");
                woman.add("Woman8");

                Collections.shuffle(man);
                Collections.shuffle(woman);

                System.out.println("--after sorting--");
                System.out.println(woman.get(0) + "| "+ man.get(0) + "| "+ man.get(1) + "| "+ man.get(2));
                System.out.println(woman.get(1) + "| "+ man.get(3) + "| "+ man.get(4) + "| "+ woman.get(2));
                System.out.println(man.get(5) + "| "+ man.get(6) + "| "+ man.get(7) + "| "+ woman.get(3));
                System.out.println(woman.get(4) + "|" + woman.get(5) + "|" + man.get(8) + "| "+ man.get(9));
                System.out.println(man.get(10) + "| "+ man.get(11) + "| "+ woman.get(6) + "| "+ woman.get(7));
        }
}

Tags:

Updated: