Problems with array and erasing indexes

68 views Asked by At

Question: How would I efficiently be able to use only one person and then when the code runs again it would not assign that same student to a group. Basically not use a student twice. I attempted to fix this problem however it ended me in a stuck dead end. Any help is appreciated, thanks!

Create an Array of the students in this class (First and Last Name).

Then randomly choose a student and place them in groups.

Group 1 - 4 Students

Group 2 - 4 Students

Group 3 - 3 Students

Group 4 - 4 Students

Group 5 - 4 Students

Group 6 - 4 Students

Then Print out the groups in an organized output. Code:

import java.util.*;

public class RandomGroupsTest {

static String[] group = {"Group 1","Group 2","Group 3", "Group 4","Group 5", "Group 6"};

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String[] students= {"Krish", "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul", "Matthew Guan", "Maysin Huang", "Kashan Karvadi", "Kilan Manivannan", "Dine Marin", "Dhruv Patel", "Mans Patel", "Vin Pham", "Sonyiea Rayman", "Mahi Sangavarapu", "Reyun Shah", "Prevan Shanmugaraj", "Riya Singh", "Palk Singh", "Rohan Singh", "Callvin Stapleton", "Arjun Vyas", "John Williams", "Alisha White" };


    Random rn = new Random();
    int a =0;
    int i = 0;
    group[i] = "Group 1";
    System.out.print("Group 1: ");
    if(group[i].equals("Group 1")) {
        for(int d = 0; d < 4; d++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }
            }
        }
        i++;
        System.out.println();
    }
    System.out.print("Group 2: ");
    if(group[i].equals("Group 2")) {
        for(int w = 0; w < 4; w++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }           
            }
        }
        i++;
        System.out.println();
    }
    System.out.print("Group 3: ");
    if(group[i].equals("Group 3")) {
        for(int v = 0; v < 3; v++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }               
            }
        }
        i++;
        System.out.println();
    }
    System.out.print("Group 4: ");
    if(group[i].equals("Group 4")) {
        for(int b = 0; b < 4; b++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }               
            }
        }
        i++;
        System.out.println();
    }
    System.out.print("Group 5: ");
    if(group[i].equals("Group 5")) {
        for(int w = 0; w < 4; w++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }               
            }
        }
        i++;
        System.out.println();
    }
    System.out.print("Group 6: ");
    if(group[i].equals("Group 6")) {
        for(int n = 0; n< 4; n++) {
             a = rn.nextInt(23);

            if(students[a]!="") {
            System.out.print("\n "+students[a]);
            students[a]= "";


            }else {
                a=rn.nextInt(23);
                if(students[a]!="") {
                System.out.println("\n "+students[a]);
                }               
            }
        }
        i++;
        System.out.println();
    }

    }

}
3

There are 3 answers

0
Arvind Kumar Avinash On BEST ANSWER

It can easily be solved using one-dimensional arrays and one-level deep nested loops. The solution given below is fairly simple to understand:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        String[] students = { "Krish", "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul", "Matthew Guan", "Maysin Huang",
                "Kashan Karvadi", "Kilan Manivannan", "Dine Marin", "Dhruv Patel", "Mans Patel", "Vin Pham",
                "Sonyiea Rayman", "Mahi Sangavarapu", "Reyun Shah", "Prevan Shanmugaraj", "Riya Singh", "Palk Singh",
                "Rohan Singh", "Callvin Stapleton", "Arjun Vyas", "John Williams", "Alisha White" };
        int[] groupCapacity = { 4, 4, 3, 4, 4, 4 };
        int[] indexUsed = new int[students.length];
        Random rn = new Random();
        int n;
        for (int i = 0; i < groupCapacity.length; i++) {
            System.out.println("Group" + (i + 1 + ": "));
            for (int j = 0; j < groupCapacity[i]; j++) {
                n = rn.nextInt(23);
                if (indexUsed[n] != 1) {
                    System.out.println(students[n]);
                    indexUsed[n] = 1;
                } else {
                    j--;
                }
            }
            System.out.println();
        }
    }
}

A sample run:

Group1: 
Maysin Huang
Reyun Shah
Callvin Stapleton
Kashan Karvadi

Group2: 
Dine Marin
Prevan Shanmugaraj
Arjun Vyas
Rohan Singh

Group3: 
Vin Pham
Mami Bhat
Matthew Guan

Group4: 
Mahi Sangavarapu
Kilan Manivannan
Palk Singh
Dhruv Patel

Group5: 
John Williams
Krish
Riya Singh
Mans Patel

Group6: 
Alisha White
Kilar Boggavarapu
Sonyiea Rayman
Nickel Gokul

Feel free to comment in case of any doubt/issue.

0
WJS On

The easiest way to pick a student randomly is to put them in a list like so:

    List<String> students = new ArrayList<>(List.of("Krish",
                "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul",
                "Matthew Guan", "Maysin Huang", "Kashan Karvadi",
                "Kilan Manivannan", "Dine Marin", "Dhruv Patel",
                "Mans Patel", "Vin Pham", "Sonyiea Rayman",
                "Mahi Sangavarapu", "Reyun Shah",
                "Prevan Shanmugaraj", "Riya Singh", "Palk Singh",
                "Rohan Singh", "Callvin Stapleton", "Arjun Vyas",
                "John Williams", "Alisha White"));

Then shuffle the list.

        Collections.shuffle(students);

Now just access the list sequentially from 0 to students.size(), using students.get().

If you are only allowed to use arrays, you can do it like this.

        String[] students = {"Krish",
                "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul",
                "Matthew Guan", "Maysin Huang", "Kashan Karvadi",
                "Kilan Manivannan", "Dine Marin", "Dhruv Patel",
                "Mans Patel", "Vin Pham", "Sonyiea Rayman",
                "Mahi Sangavarapu", "Reyun Shah",
                "Prevan Shanmugaraj", "Riya Singh", "Palk Singh",
                "Rohan Singh", "Callvin Stapleton", "Arjun Vyas",
                "John Williams", "Alisha White"};

        int length = students.length;
        Random r = new Random();

        public String getNextStudent() {
            if (length == 0) {
                return null;
            }
            // pick a number from 0 to length
             int pick = r.nextInt(length);
             // get that student
             String student = students[pick];
             // copy last student to picked students slot
             students[pick] = students[length-1];
             // update length
             length--;
             return student;
        }

If you receive a null value then no more students. Note that this will destroy the list. If you want to save the list, then simply swap the last student with the one picked. The resulting list itself will be randomized.

0
Jason On

Maintain a List of all String objects. I would normally use a Set for efficiency but we need to get a random element from the Collection fairly fast so a List seems appropriate. On each creation we then remove the List of Strings from the underlying list. There does seem to be 3 names left over for some reason (heads up)!

    public static void main(String[] args) {
        List<String> names = new ArrayList<>(Arrays.asList(
                "Krish", "Mami Bhat", "Kilar Boggavarapu", "Nickel Gokul",
                "Matthew Guan", "Maysin Huang", "Kashan Karvadi", "Kilan Manivannan",
                "Dine Marin", "Dhruv Patel", "Mans Patel", "Vin Pham", "Sonyiea Rayman",
                "Mahi Sangavarapu", "Reyun Shah", "Prevan Shanmugaraj", "Riya Singh",
                "Palk Singh", "Rohan Singh", "Callvin Stapleton", "Arjun Vyas",
                "John Williams", "Alisha White"));

        Random random = new Random();

        List<List<String>> groups = groups(random, names, 4, 4, 3, 4, 4, 4);

        for (List<String> group : groups) {
            System.out.println("Group: " + group);
        }
    }

    static List<List<String>> groups(Random random, List<String> names, int... groupSizes) {
        return IntStream.of(groupSizes).mapToObj(size -> createGroup(random, names, size)).collect(Collectors.toList());
    }

    static List<String> createGroup(Random random, List<String> names, int size) {
        List<String> result = new ArrayList<>(size);

        while (size-- > 0) {
            int index = random.nextInt(names.size());

            result.add(names.get(index));
        }
        names.removeAll(result);

        return result;
    }