Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: ArrayList troubles

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile ArrayList troubles

    Hey, I'm starting a new thread since my thread "Using Random class" may or may not have the error due to the class Random. More likely it's something wrong with one of my ArrayLists.

    package DiscreteMathProject;
     
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;
     
    public class MainClass {
     
        private String[] studentNames;
        private Student[] students;
        private Class[] classes;
        private String[] classNames;
        private ArrayList<Student> aList2;
        private Random[] random;
        PrintWriter outFile;
        public MainClass() throws FileNotFoundException
        {
            outFile = new PrintWriter("classes.txt");
            classes = new Class[35];
            studentNames = new String[500];
            students = new Student[500];
            studentNames[0] = "Joe McDonald";
            studentNames[1] = "Paul Adcock";
            studentNames[2] = "Darth Vader";
            studentNames[3] = "Peter Parker";
            studentNames[4] = "Abraham Lincoln";
            studentNames[5] = "Albert Einstein";
            studentNames[6] = "Spongebob Squarepants";
            studentNames[7] = "James Bond";
            studentNames[8] = "Sherlock Holmes";
            studentNames[9] = "John Wayne";
            studentNames[10] = "Walter Payton";
            studentNames[11] = "George Herman (Babe) Ruth";
            studentNames[12] = "Susan B. Anthony";
            studentNames[13] = "Martin Luther";
            studentNames[14] = "Julius Ceasar";
            studentNames[15] = "Adolf Hitler";
            studentNames[16] = "Joseph Stalin";
            studentNames[17] = "Fidel Castro";
            studentNames[18] = "Ivan The Terrible";
            studentNames[19] = "Jabba The Hutt";
            studentNames[20] = "Luke Skywalker";
            studentNames[21] = "Emperor Palpatine";
            studentNames[22] = "Thomas Edison";
            studentNames[23] = "Benjamin Franklin";
            studentNames[24] = "Judas Iscariot";
            studentNames[25] = "King David";
            studentNames[26] = "Blaise Pascal";
            studentNames[27] = "John Watson";
            studentNames[28] = "James Watt";
            studentNames[29] = "Henry Ford";
            studentNames[30] = "Michael Jackson";
            studentNames[31] = "Mohatma Ghandi";
            studentNames[32] = "Emperor Mao";
            studentNames[33] = "Robin of Locksley (Robin Hood)";
            studentNames[34] = "King Richard The Lionhearted";
            studentNames[35] = "Harry Potter";
            studentNames[36] = "John Smith";
            studentNames[37] = "King John";
            studentNames[38] = "William The Conqueror";
            studentNames[39] = "Bloody Mary";
            studentNames[40] = "Johnny Cash";
            studentNames[41] = "Sargon of Akkad";
            studentNames[42] = "Prince Absalom";
            studentNames[43] = "Eustace Clarence";
            studentNames[44] = "Ronald Weasley";
            studentNames[45] = "Rosa Parks";
            studentNames[46] = "Sandra Day O'Connor";
            studentNames[47] = "Queen Lucy Pevensie";
            studentNames[48] = "John The Apostle";
            studentNames[49] = "Kevin McAlister";
            studentNames[50] = "Alex Pruitt";
            studentNames[51] = "Guy Fawkes";
            studentNames[52] = "Queen Heshepshut";
            studentNames[53] = "King Ramases";
            studentNames[54] = "King Xerxes of Persia";
            studentNames[55] = "Nathan Hale";
            studentNames[56] = "Patrick Henry";
            studentNames[57] = "Jacob Marley";
            studentNames[58] = "Ebenezer Scrooge";
            studentNames[59] = "Saint Augustine";
            studentNames[60] = "John Lennon";
            studentNames[61] = "Forrest Gump";
            studentNames[62] = "Jenny Gump";
            studentNames[63] = "Luitenant Dan";
            studentNames[64] = "John Q. Public";
            studentNames[65] = "Bill Gates";
            studentNames[66] = "Harriet Tubman";
            studentNames[67] = "Sojourner Truth";
            studentNames[68] = "Malcom Little(Malcom X)";
            studentNames[69] = "Leonardo D. Caprio";
            studentNames[70] = "Jack Philps";
            studentNames[71] = "Paul Revere";
            studentNames[72] = "Becky Thatcher";
            studentNames[73] = "Tom Sawyer";
            studentNames[74] = "Samual Clemmons(Mark Twain)";
            studentNames[75] = "George Washington Carver";
            studentNames[76] = "Paul Bunyan";
            studentNames[77] = "Huck Finn";
            studentNames[78] = "Picos Bill";
            studentNames[79] = "Mark Anthony";
            studentNames[80] = "Queen Cleopatra";
            studentNames[81] = "Anne Frank";
            studentNames[82] = "Eli Wesel";
            studentNames[83] = "Eddie Brock";
            studentNames[84] = "Thorin Oakenshield";
            studentNames[85] = "Bilbo Bagins";
            studentNames[86] = "Frodo Baggins";
            studentNames[87] = "Sam Gamagee";
            studentNames[88] = "Gandalf the Gray";
            studentNames[89] = "Sauraman the Wise";
            studentNames[90] = "King George III";
            studentNames[91] = "William Wilberforce";
            studentNames[92] = "Merry Brandybuck";
            studentNames[93] = "Martin Luther King Jr.";
            studentNames[94] = "Jackie Robinson";
            studentNames[95] = "Sherwim Williams";
            studentNames[96] = "John Menard";
            studentNames[97] = "Chrisopher Columbus";
            studentNames[98] = "Genghis Khan";
            studentNames[99] = "Atilla The Hun";
            studentNames[100] = "Al Capone";
            studentNames[101] = "Napolean Bonepart";
            studentNames[102] = "Marie Antionette";
            studentNames[103] = "King Louis XVII";
            studentNames[104] = "Napolean Blownapart";
            studentNames[105] = "Yesser Arafat";
            studentNames[106] = "Al Gore";
            studentNames[107] = "Benedict Arnold";
            studentNames[108] = "Tim Allen";
            studentNames[109] = "George W. Bush";
            for (int x = 110; x < 500; x++)
            {
                studentNames[x] = "Student " + x;
            }
     
            for (int y = 0; y < 500; y++)
            {
                students[y] = new Student(studentNames[y]);
     
            }
     
            classNames = new String[35];
     
            classNames[0] = "Sleeping 101";
            classNames[1] = "Partying 202";
            classNames[2] = "Discrete Math";
            classNames[3] = "Rocket Science";
            classNames[4] = "Introduction to Data Structures";
            classNames[5] = "Skipping 303";
            classNames[6] = "Creative Writing";
            classNames[7] = "Introduction to Technical Communication";
            classNames[8] = "Easy Class";
            classNames[9] = "Surviving College";
            classNames[10] = "How To Outsmart the Profs";
            classNames[11] = "Astronomy";
            classNames[12] = "Thermodynamics";
            classNames[13] = "Long-winded Lectures 404";
            classNames[14] = "System Design";
            classNames[15] = "Physics";
            classNames[16] = "Chemistry";
            classNames[17] = "Tutoring";
            classNames[18] = "Politics 204";
            classNames[19] = "Very Hard Class";
            classNames[20] = "Introduction to Java Programming";
            classNames[21] = "Biology";
            classNames[22] = "Physical Education";
            classNames[23] = "Databases";
            classNames[24] = "World History";
            classNames[25] = "Philosophy";
            classNames[26] = "Daydreaming 404";
            classNames[27] = "Eating 208";
            classNames[28] = "Kung Fu Fighting";
            classNames[29] = "Religion";
            classNames[30] = "Film Making";
            classNames[31] = "How to Understand Women(Sorta) 467";
            classNames[32] = "How to Understand Men(Sorta) 468";
            classNames[33] = "TV Studio 303";
            classNames[34] = "Architecture";
     
            for (int z = 0; z < 35; z++)
            {
                classes[z] = new Class(classNames[z]);
     
            }
           System.out.println(classes[0].getName());
     
    Random rand1 = new Random();
     Set<Student> mix1 = new TreeSet<Student>();
    Student[] st1 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[0].getSize(); i++ ){
    int rN1 = rand1.nextInt(classes[0].getSize());
    while ( mix1.contains(students[rN1]) ){//already chose
    rN1= rand1.nextInt(classes[0].getSize());//get another
    } 
    mix1.add(students[rN1]);
    mix1.toArray(st1);
     st1 = mix1.toArray(st1);
     }
     for (int k = 0; k < classes[0].getSize()  ; k++)
    {
      classes[0].addStudents(st1[k]);
    }
    System.out.println(classes[0].toString());
    outFile.println(classes[0].toString());
    Random rand2 = new Random();
     Set<Student> mix2 = new TreeSet<Student>();
    Student[] st2 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[1].getSize(); i++ ){
    int rN2 = rand2.nextInt(classes[1].getSize());
    while ( mix2.contains(students[rN2]) ){//already chose
    rN2= rand2.nextInt(classes[1].getSize());//get another
    } 
    mix2.add(students[rN2]);
    mix2.toArray(st2);
     st2 = mix2.toArray(st2);
     }
     for (int k = 0; k < classes[1].getSize()  ; k++)
    {
      classes[1].addStudents(st2[k]);
    }
    System.out.println(classes[1].toString());
    outFile.println(classes[1].toString());
    Random rand3 = new Random();
     Set<Student> mix3 = new TreeSet<Student>();
    Student[] st3 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[2].getSize(); i++ ){
    int rN3 = rand3.nextInt(classes[2].getSize());
    while ( mix3.contains(students[rN3]) ){//already chose
    rN3= rand3.nextInt(classes[2].getSize());//get another
    } 
    mix3.add(students[rN3]);
    mix3.toArray(st3);
     st3 = mix3.toArray(st3);
     }
     for (int k = 0; k < classes[2].getSize()  ; k++)
    {
      classes[2].addStudents(st3[k]);
    }
    System.out.println(classes[2].toString());
    outFile.println(classes[2].toString());
    Random rand4 = new Random();
     Set<Student> mix4 = new TreeSet<Student>();
    Student[] st4 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[3].getSize(); i++ ){
    int rN4 = rand4.nextInt(classes[3].getSize());
    while ( mix4.contains(students[rN4]) ){//already chose
    rN4= rand4.nextInt(classes[3].getSize());//get another
    } 
    mix4.add(students[rN4]);
    mix4.toArray(st4);
     st4 = mix4.toArray(st4);
     }
     for (int k = 0; k < classes[3].getSize()  ; k++)
    {
      classes[3].addStudents(st4[k]);
    }
    System.out.println(classes[3].toString());
    outFile.println(classes[3].toString());
    Random rand5 = new Random();
     Set<Student> mix5 = new TreeSet<Student>();
    Student[] st5 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[4].getSize(); i++ ){
    int rN5 = rand5.nextInt(classes[4].getSize());
    while ( mix5.contains(students[rN5]) ){//already chose
    rN5= rand5.nextInt(classes[4].getSize());//get another
    } 
    mix5.add(students[rN5]);
    mix5.toArray(st5);
     st5 = mix5.toArray(st5);
     }
     for (int k = 0; k < classes[4].getSize()  ; k++)
    {
      classes[4].addStudents(st5[k]);
    }
    System.out.println(classes[4].toString());
    outFile.println(classes[4].toString());
    Random rand6 = new Random();
     Set<Student> mix6 = new TreeSet<Student>();
    Student[] st6 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[5].getSize(); i++ ){
    int rN6 = rand6.nextInt(classes[5].getSize());
    while ( mix6.contains(students[rN6]) ){//already chose
    rN6= rand6.nextInt(classes[5].getSize());//get another
    } 
    mix6.add(students[rN6]);
    mix6.toArray(st6);
     st6 = mix6.toArray(st6);
     }
     for (int k = 0; k < classes[5].getSize()  ; k++)
    {
      classes[5].addStudents(st6[k]);
    }
    System.out.println(classes[5].toString());
    outFile.println(classes[5].toString());
    Random rand7 = new Random();
     Set<Student> mix7 = new TreeSet<Student>();
    Student[] st7 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[6].getSize(); i++ ){
    int rN7 = rand7.nextInt(classes[6].getSize());
    while ( mix7.contains(students[rN7]) ){//already chose
    rN7= rand7.nextInt(classes[6].getSize());//get another
    } 
    mix7.add(students[rN7]);
    mix7.toArray(st7);
     st7 = mix7.toArray(st7);
     }
     for (int k = 0; k < classes[6].getSize()  ; k++)
    {
      classes[6].addStudents(st7[k]);
    }
    System.out.println(classes[6].toString());
    outFile.println(classes[6].toString());
    Random rand8 = new Random();
     Set<Student> mix8 = new TreeSet<Student>();
    Student[] st8 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[7].getSize(); i++ ){
    int rN8 = rand8.nextInt(classes[7].getSize());
    while ( mix8.contains(students[rN8]) ){//already chose
    rN8= rand8.nextInt(classes[7].getSize());//get another
    } 
    mix8.add(students[rN8]);
    mix8.toArray(st8);
     st8 = mix8.toArray(st8);
     }
     for (int k = 0; k < classes[7].getSize()  ; k++)
    {
      classes[7].addStudents(st8[k]);
    }
    System.out.println(classes[7].toString());
    outFile.println(classes[7].toString());
    Random rand9 = new Random();
     Set<Student> mix9 = new TreeSet<Student>();
    Student[] st9 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[8].getSize(); i++ ){
    int rN9 = rand9.nextInt(classes[8].getSize());
    while ( mix9.contains(students[rN9]) ){//already chose
    rN9= rand9.nextInt(classes[8].getSize());//get another
    } 
    mix9.add(students[rN9]);
    mix9.toArray(st9);
     st9 = mix9.toArray(st9);
     }
     for (int k = 0; k < classes[8].getSize()  ; k++)
    {
      classes[8].addStudents(st9[k]);
    }
    System.out.println(classes[8].toString());
    outFile.println(classes[8].toString());
    Random rand10 = new Random();
     Set<Student> mix10 = new TreeSet<Student>();
    Student[] st10 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[9].getSize(); i++ ){
    int rN10 = rand10.nextInt(classes[9].getSize());
    while ( mix10.contains(students[rN10]) ){//already chose
    rN10= rand10.nextInt(classes[9].getSize());//get another
    } 
    mix10.add(students[rN10]);
    mix10.toArray(st10);
     st10 = mix10.toArray(st10);
     }
     for (int k = 0; k < classes[9].getSize()  ; k++)
    {
      classes[9].addStudents(st10[k]);
    }
    System.out.println(classes[9].toString());
    outFile.println(classes[9].toString());
    Random rand11 = new Random();
     Set<Student> mix11 = new TreeSet<Student>();
    Student[] st11 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[10].getSize(); i++ ){
    int rN11 = rand11.nextInt(classes[10].getSize());
    while ( mix11.contains(students[rN11]) ){//already chose
    rN11= rand11.nextInt(classes[10].getSize());//get another
    } 
    mix11.add(students[rN11]);
    mix11.toArray(st11);
     st11 = mix11.toArray(st11);
     }
     for (int k = 0; k < classes[10].getSize()  ; k++)
    {
      classes[10].addStudents(st11[k]);
    }
    System.out.println(classes[10].toString());
    outFile.println(classes[10].toString());
    Random rand12 = new Random();
     Set<Student> mix12 = new TreeSet<Student>();
    Student[] st12 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[11].getSize(); i++ ){
    int rN12 = rand12.nextInt(classes[11].getSize());
    while ( mix12.contains(students[rN12]) ){//already chose
    rN12= rand12.nextInt(classes[11].getSize());//get another
    } 
    mix12.add(students[rN12]);
    mix12.toArray(st12);
     st12 = mix12.toArray(st12);
     }
     for (int k = 0; k < classes[11].getSize()  ; k++)
    {
      classes[11].addStudents(st12[k]);
    }
    System.out.println(classes[11].toString());
    outFile.println(classes[11].toString());
    Random rand13 = new Random();
     Set<Student> mix13 = new TreeSet<Student>();
    Student[] st13 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[12].getSize(); i++ ){
    int rN13 = rand13.nextInt(classes[12].getSize());
    while ( mix13.contains(students[rN13]) ){//already chose
    rN13= rand13.nextInt(classes[12].getSize());//get another
    } 
    mix13.add(students[rN13]);
    mix13.toArray(st13);
     st13 = mix13.toArray(st13);
     }
     for (int k = 0; k < classes[12].getSize()  ; k++)
    {
      classes[12].addStudents(st13[k]);
    }
    System.out.println(classes[12].toString());
    outFile.println(classes[12].toString());
    Random rand14 = new Random();
     Set<Student> mix14 = new TreeSet<Student>();
    Student[] st14 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[13].getSize(); i++ ){
    int rN14 = rand14.nextInt(classes[13].getSize());
    while ( mix14.contains(students[rN14]) ){//already chose
    rN14= rand14.nextInt(classes[13].getSize());//get another
    } 
    mix14.add(students[rN14]);
    mix14.toArray(st14);
     st14 = mix14.toArray(st14);
     }
     for (int k = 0; k < classes[13].getSize()  ; k++)
    {
      classes[13].addStudents(st14[k]);
    }
    System.out.println(classes[13].toString());
    outFile.println(classes[13].toString());
    Random rand15 = new Random();
     Set<Student> mix15 = new TreeSet<Student>();
    Student[] st15 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[14].getSize(); i++ ){
    int rN15 = rand15.nextInt(classes[14].getSize());
    while ( mix15.contains(students[rN15]) ){//already chose
    rN15= rand15.nextInt(classes[14].getSize());//get another
    } 
    mix15.add(students[rN15]);
    mix15.toArray(st15);
     st15 = mix15.toArray(st15);
     }
     for (int k = 0; k < classes[14].getSize()  ; k++)
    {
      classes[14].addStudents(st15[k]);
    }
    System.out.println(classes[14].toString());
    outFile.println(classes[14].toString());
    Random rand16 = new Random();
     Set<Student> mix16 = new TreeSet<Student>();
    Student[] st16 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[15].getSize(); i++ ){
    int rN16 = rand16.nextInt(classes[15].getSize());
    while ( mix16.contains(students[rN16]) ){//already chose
    rN16= rand16.nextInt(classes[15].getSize());//get another
    } 
    mix16.add(students[rN16]);
    mix16.toArray(st16);
     st16 = mix16.toArray(st16);
     }
     for (int k = 0; k < classes[15].getSize()  ; k++)
    {
      classes[15].addStudents(st16[k]);
    }
    System.out.println(classes[15].toString());
    outFile.println(classes[15].toString());
    Random rand17 = new Random();
     Set<Student> mix17 = new TreeSet<Student>();
    Student[] st17 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[16].getSize(); i++ ){
    int rN17 = rand17.nextInt(classes[16].getSize());
    while ( mix17.contains(students[rN17]) ){//already chose
    rN17= rand17.nextInt(classes[16].getSize());//get another
    } 
    mix17.add(students[rN17]);
    mix17.toArray(st17);
     st17 = mix17.toArray(st17);
     }
     for (int k = 0; k < classes[16].getSize()  ; k++)
    {
      classes[16].addStudents(st17[k]);
    }
    System.out.println(classes[16].toString());
    outFile.println(classes[16].toString());
    Random rand18 = new Random();
     Set<Student> mix18 = new TreeSet<Student>();
    Student[] st18 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[17].getSize(); i++ ){
    int rN18 = rand18.nextInt(classes[17].getSize());
    while ( mix18.contains(students[rN18]) ){//already chose
    rN18= rand18.nextInt(classes[17].getSize());//get another
    } 
    mix18.add(students[rN18]);
    mix18.toArray(st18);
     st18 = mix18.toArray(st18);
     }
     for (int k = 0; k < classes[17].getSize()  ; k++)
    {
      classes[17].addStudents(st18[k]);
    }
    System.out.println(classes[17].toString());
    outFile.println(classes[17].toString());
    Random rand19 = new Random();
     Set<Student> mix19 = new TreeSet<Student>();
    Student[] st19 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[18].getSize(); i++ ){
    int rN19 = rand19.nextInt(classes[18].getSize());
    while ( mix19.contains(students[rN19]) ){//already chose
    rN19= rand19.nextInt(classes[18].getSize());//get another
    } 
    mix19.add(students[rN19]);
    mix19.toArray(st19);
     st19 = mix19.toArray(st19);
     }
     for (int k = 0; k < classes[18].getSize()  ; k++)
    {
      classes[18].addStudents(st19[k]);
    }
    System.out.println(classes[18].toString());
    outFile.println(classes[18].toString());
    Random rand20 = new Random();
     Set<Student> mix20 = new TreeSet<Student>();
    Student[] st20 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[19].getSize(); i++ ){
    int rN20 = rand20.nextInt(classes[19].getSize());
    while ( mix20.contains(students[rN20]) ){//already chose
    rN20= rand20.nextInt(classes[19].getSize());//get another
    } 
    mix20.add(students[rN20]);
    mix20.toArray(st20);
     st20 = mix20.toArray(st20);
     }
     for (int k = 0; k < classes[19].getSize()  ; k++)
    {
      classes[19].addStudents(st20[k]);
    }
    System.out.println(classes[19].toString());
    outFile.println(classes[19].toString());
    Random rand21 = new Random();
     Set<Student> mix21 = new TreeSet<Student>();
    Student[] st21 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[20].getSize(); i++ ){
    int rN21 = rand21.nextInt(classes[20].getSize());
    while ( mix21.contains(students[rN21]) ){//already chose
    rN21= rand21.nextInt(classes[20].getSize());//get another
    } 
    mix21.add(students[rN21]);
    mix21.toArray(st21);
     st21 = mix21.toArray(st21);
     }
     for (int k = 0; k < classes[20].getSize()  ; k++)
    {
      classes[20].addStudents(st21[k]);
    }
    System.out.println(classes[20].toString());
    outFile.println(classes[20].toString());
    Random rand22 = new Random();
     Set<Student> mix22 = new TreeSet<Student>();
    Student[] st22 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[21].getSize(); i++ ){
    int rN22 = rand22.nextInt(classes[21].getSize());
    while ( mix22.contains(students[rN22]) ){//already chose
    rN22= rand22.nextInt(classes[21].getSize());//get another
    } 
    mix22.add(students[rN22]);
    mix22.toArray(st22);
     st22 = mix22.toArray(st22);
     }
     for (int k = 0; k < classes[21].getSize()  ; k++)
    {
      classes[21].addStudents(st22[k]);
    }
    System.out.println(classes[21].toString());
    outFile.println(classes[21].toString());
    Random rand23 = new Random();
     Set<Student> mix23 = new TreeSet<Student>();
    Student[] st23 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[22].getSize(); i++ ){
    int rN23 = rand23.nextInt(classes[22].getSize());
    while ( mix23.contains(students[rN23]) ){//already chose
    rN23= rand23.nextInt(classes[22].getSize());//get another
    } 
    mix23.add(students[rN23]);
    mix23.toArray(st23);
     st23 = mix23.toArray(st23);
     }
     for (int k = 0; k < classes[22].getSize()  ; k++)
    {
      classes[22].addStudents(st23[k]);
    }
    System.out.println(classes[22].toString());
    outFile.println(classes[22].toString());
    Random rand24 = new Random();
     Set<Student> mix24 = new TreeSet<Student>();
    Student[] st24 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[23].getSize(); i++ ){
    int rN24 = rand24.nextInt(classes[23].getSize());
    while ( mix24.contains(students[rN24]) ){//already chose
    rN24= rand24.nextInt(classes[23].getSize());//get another
    } 
    mix24.add(students[rN24]);
    mix24.toArray(st24);
     st24 = mix24.toArray(st24);
     }
     for (int k = 0; k < classes[23].getSize()  ; k++)
    {
      classes[23].addStudents(st24[k]);
    }
    System.out.println(classes[23].toString());
    outFile.println(classes[23].toString());
    Random rand25 = new Random();
     Set<Student> mix25 = new TreeSet<Student>();
    Student[] st25 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[24].getSize(); i++ ){
    int rN25 = rand25.nextInt(classes[24].getSize());
    while ( mix25.contains(students[rN25]) ){//already chose
    rN25= rand25.nextInt(classes[24].getSize());//get another
    } 
    mix25.add(students[rN25]);
    mix25.toArray(st25);
     st25 = mix25.toArray(st25);
     }
     for (int k = 0; k < classes[24].getSize()  ; k++)
    {
      classes[24].addStudents(st25[k]);
    }
    System.out.println(classes[24].toString());
    outFile.println(classes[24].toString());
    Random rand26 = new Random();
     Set<Student> mix26 = new TreeSet<Student>();
    Student[] st26 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[25].getSize(); i++ ){
    int rN26 = rand26.nextInt(classes[25].getSize());
    while ( mix26.contains(students[rN26]) ){//already chose
    rN26= rand26.nextInt(classes[25].getSize());//get another
    } 
    mix26.add(students[rN26]);
    mix26.toArray(st26);
     st26 = mix26.toArray(st26);
     }
     for (int k = 0; k < classes[25].getSize()  ; k++)
    {
      classes[25].addStudents(st26[k]);
    }
    System.out.println(classes[25].toString());
    outFile.println(classes[25].toString());
    Random rand27 = new Random();
     Set<Student> mix27 = new TreeSet<Student>();
    Student[] st27 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[26].getSize(); i++ ){
    int rN27 = rand27.nextInt(classes[26].getSize());
    while ( mix27.contains(students[rN27]) ){//already chose
    rN27= rand27.nextInt(classes[26].getSize());//get another
    } 
    mix27.add(students[rN27]);
    mix27.toArray(st27);
     st27 = mix27.toArray(st27);
     }
     for (int k = 0; k < classes[26].getSize()  ; k++)
    {
      classes[26].addStudents(st27[k]);
    }
    System.out.println(classes[26].toString());
    outFile.println(classes[26].toString());
    Random rand28 = new Random();
     Set<Student> mix28 = new TreeSet<Student>();
    Student[] st28 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[27].getSize(); i++ ){
    int rN28 = rand28.nextInt(classes[27].getSize());
    while ( mix28.contains(students[rN28]) ){//already chose
    rN28= rand28.nextInt(classes[27].getSize());//get another
    } 
    mix28.add(students[rN28]);
    mix28.toArray(st28);
     st28 = mix28.toArray(st28);
     }
     for (int k = 0; k < classes[27].getSize()  ; k++)
    {
      classes[27].addStudents(st28[k]);
    }
    System.out.println(classes[27].toString());
    outFile.println(classes[27].toString());
    Random rand29 = new Random();
     Set<Student> mix29 = new TreeSet<Student>();
    Student[] st29 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[28].getSize(); i++ ){
    int rN29 = rand29.nextInt(classes[28].getSize());
    while ( mix29.contains(students[rN29]) ){//already chose
    rN29= rand29.nextInt(classes[28].getSize());//get another
    } 
    mix29.add(students[rN29]);
    mix29.toArray(st29);
     st29 = mix29.toArray(st29);
     }
     for (int k = 0; k < classes[28].getSize()  ; k++)
    {
      classes[28].addStudents(st29[k]);
    }
    System.out.println(classes[28].toString());
    outFile.println(classes[28].toString());
    Random rand30 = new Random();
     Set<Student> mix30 = new TreeSet<Student>();
    Student[] st30 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[29].getSize(); i++ ){
    int rN30 = rand30.nextInt(classes[29].getSize());
    while ( mix30.contains(students[rN30]) ){//already chose
    rN30= rand30.nextInt(classes[29].getSize());//get another
    } 
    mix30.add(students[rN30]);
    mix30.toArray(st30);
     st30 = mix30.toArray(st30);
     }
     for (int k = 0; k < classes[29].getSize()  ; k++)
    {
      classes[29].addStudents(st30[k]);
    }
    System.out.println(classes[29].toString());
    outFile.println(classes[29].toString());
    Random rand31 = new Random();
     Set<Student> mix31 = new TreeSet<Student>();
    Student[] st31 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[30].getSize(); i++ ){
    int rN31 = rand31.nextInt(classes[30].getSize());
    while ( mix31.contains(students[rN31]) ){//already chose
    rN31= rand31.nextInt(classes[30].getSize());//get another
    } 
    mix31.add(students[rN31]);
    mix31.toArray(st31);
     st31 = mix31.toArray(st31);
     }
     for (int k = 0; k < classes[30].getSize()  ; k++)
    {
      classes[30].addStudents(st31[k]);
    }
    System.out.println(classes[30].toString());
    outFile.println(classes[30].toString());
    Random rand32 = new Random();
     Set<Student> mix32 = new TreeSet<Student>();
    Student[] st32 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[31].getSize(); i++ ){
    int rN32 = rand32.nextInt(classes[31].getSize());
    while ( mix32.contains(students[rN32]) ){//already chose
    rN32= rand32.nextInt(classes[31].getSize());//get another
    } 
    mix32.add(students[rN32]);
    mix32.toArray(st32);
     st32 = mix32.toArray(st32);
     }
     for (int k = 0; k < classes[31].getSize()  ; k++)
    {
      classes[31].addStudents(st32[k]);
    }
    System.out.println(classes[31].toString());
    outFile.println(classes[31].toString());
    Random rand33 = new Random();
     Set<Student> mix33 = new TreeSet<Student>();
    Student[] st33 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[32].getSize(); i++ ){
    int rN33 = rand33.nextInt(classes[32].getSize());
    while ( mix33.contains(students[rN33]) ){//already chose
    rN33= rand33.nextInt(classes[32].getSize());//get another
    } 
    mix33.add(students[rN33]);
    mix33.toArray(st33);
     st33 = mix33.toArray(st33);
     }
     for (int k = 0; k < classes[32].getSize()  ; k++)
    {
      classes[32].addStudents(st33[k]);
    }
    System.out.println(classes[32].toString());
    outFile.println(classes[32].toString());
    Random rand34 = new Random();
     Set<Student> mix34 = new TreeSet<Student>();
    Student[] st34 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[33].getSize(); i++ ){
    int rN34 = rand34.nextInt(classes[33].getSize());
    while ( mix34.contains(students[rN34]) ){//already chose
    rN34= rand34.nextInt(classes[33].getSize());//get another
    } 
    mix34.add(students[rN34]);
    mix34.toArray(st34);
     st34 = mix34.toArray(st34);
     }
     for (int k = 0; k < classes[33].getSize()  ; k++)
    {
      classes[33].addStudents(st34[k]);
    }
    System.out.println(classes[33].toString());
    outFile.println(classes[33].toString());
    Random rand35 = new Random();
     Set<Student> mix35 = new TreeSet<Student>();
    Student[] st35 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[34].getSize(); i++ ){
    int rN35 = rand35.nextInt(classes[34].getSize());
    while ( mix35.contains(students[rN35]) ){//already chose
    rN35= rand35.nextInt(classes[34].getSize());//get another
    } 
    mix35.add(students[rN35]);
    mix35.toArray(st35);
     st35 = mix35.toArray(st35);
     }
     for (int k = 0; k < classes[34].getSize()  ; k++)
    {
      classes[34].addStudents(st35[k]);
    }
    System.out.println(classes[34].toString());
    outFile.println(classes[34].toString());
    outFile.close();
     
     
        }
     
        public static void main(String[] args) throws FileNotFoundException
        {
            MainClass mainC = new MainClass();
        }
    }

    package DiscreteMathProject;
     
    /**
     * @author pradcoc
     *
     */
    public class Student {
     
        private String name;
        private int numOfClasses;
        private ArrayList<Class> aList;
        private ArrayList<String> aList2;
     
        public Student(String name)
        {
     
            name = this.name;
             setName(name);
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String name2)
        {
            name = name2;
        }
     
       public void addClasses(Class c)
    {
    aList.add(c);
    }
     
    public String getClasses()
    {
    for (int i = 0; i < aList.size(); i++)
            {
                aList2.add(aList.get(i).getName());
            }
     
            String str = aList2.toString();
            return str;
     
    }
     
    public int getNumOfClasses()
    {
    numOfClasses = aList2.size();
    return numOfClasses;
    }
     
    public String toString()
    {
    String str = "The student's name is: " + getName() + ".  The student is in " + getNumOfClasses() + " classes.  The classes are " + getClasses() + ".";
    return str;
    }
     
    }

    package DiscreteMathProject;
     
    import java.util.ArrayList;
    import java.util.Random;
     
    /**
     * @author pradcoc
     *
     */
    public class Class {
     
        private Student[] students;
        private String[] times;
        private String name;
        private String time;
        private int size;
        private Random r,r2;
        private Random[] random;
        private ArrayList<Student> aList;
        private ArrayList<String> aList2;
     
        public Class(String name)
        {
     
            name = this.name;
            times = new String[10];
            times[0] = "8:00 a.m.";
            times[1] = "9:30 a.m.";
            times[2] = "11:00 a.m.";
            times[3] = "12:30 p.m.";
            times[4] = "2:00 p.m.";
            times[5] = "3:30 p.m.";
            times[6] = "5:00 p.m.";
            times[7] = "6:30 p.m.";
            times[8] = "8:00 p.m.";
            times[9] = "9:30 p.m.";
            aList = new ArrayList<Student>();
       aList2 = new ArrayList<String>();
          setName(name);
     
     
        }
        public String getTime()
        {
            r = new Random();
            int x = r.nextInt(10);
            time= times[x];
            return time;
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String name2)
        {
            name = name2;
        }
     
        public void addStudents(Student s)
        {
     
            aList.add(s);
     
        }
     
    public String  getAdjacentClasses(Class[] classes)
    {
    ArrayList<String> penguinList = new ArrayList<String>();
     
    for (int i = 0; i < classes.length; i++)
    {
     
    // how do I get it so that it checks the getArrayList2() method of each class and checks to see if
    // the get method, for any index, returns true, i.e., they have a Student in common
    // I'm baffled how to do it without it throwing some exception somewhere or making a glitch somewhere
     
    }
     
    }
     
        public ArrayList<Student> getArrayList()
        {
            return aList;
        }
     
        public int getSize()
        {
        r2 = new Random();
     
        int x = r2.nextInt(26);
        if (x < 15)
        {
            x = 15;
            size = x;
            return size;
        }
     
     
        else
        {
        size = x;
        return size;
        }
        }
     
        public String toString()
        {
            String str = "The class: " + getName() + " is at " + getTime() + "and has " + getSize() + " students." +
            " The students in it are: " + getStudentsInClass() + "." ;
            return str;
        }
     
        public String getStudentsInClass()
        {
     
            for (int i = 0; i < getSize(); i++)
            {
                aList2.add(aList.get(i).getName());
            }
     
            String str = aList2.toString();
            return str;
        }
     
    public ArrayList<String> getStudentsInClass2()
    {
    return aList2;
    }
    }

    For starters, what's causing the ArrayIndexOutOfBoundsException ?

    Using Random class
    Last edited by javapenguin; November 18th, 2010 at 03:04 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

     

    The class: Partying 202 is at 12:30 p.m.and has 15 students. The students in it are: [Darth Vader, Sherlock Holmes, Joe McDonald, Martin Luther, Abraham Lincoln, Albert Einstein, Julius Ceasar, George Herman (Babe) Ruth, Susan B. Anthony, Spongebob Squarepants, Peter Parker, Paul Adcock, John Wayne, Adolf Hitler, Walter Payton].
    The class: Discrete Math is at 8:00 a.m.and has 15 students. The students in it are: [Darth Vader, Sherlock Holmes, Joe McDonald, Jabba The Hutt, Martin Luther, Abraham Lincoln, Albert Einstein, Julius Ceasar, Spongebob Squarepants, George Herman (Babe) Ruth, Peter Parker, John Wayne, Paul Adcock, Walter Payton, James Bond].
    The class: Rocket Science is at 9:30 a.m.and has 15 students. The students in it are: [Darth Vader, Joe McDonald, Abraham Lincoln, Emperor Palpatine, Albert Einstein, Julius Ceasar, Spongebob Squarepants, Susan B. Anthony, George Herman (Babe) Ruth, Peter Parker, Joseph Stalin, John Wayne, Paul Adcock, Adolf Hitler, James Bond].
    The class: Introduction to Data Structures is at 8:00 p.m.and has 15 students. The students in it are: [Sherlock Holmes, Darth Vader, Thomas Edison, Luke Skywalker, Abraham Lincoln, Martin Luther, Emperor Palpatine, Albert Einstein, Julius Ceasar, George Herman (Babe) Ruth, Susan B. Anthony, Peter Parker, Joseph Stalin, John Wayne, Paul Adcock].
    The class: Skipping 303 is at 12:30 p.m.and has 15 students. The students in it are: [Darth Vader, Sherlock Holmes, Joe McDonald, Jabba The Hutt, Luke Skywalker, Abraham Lincoln, Albert Einstein, Julius Ceasar, Susan B. Anthony, Spongebob Squarepants, George Herman (Babe) Ruth, Ivan The Terrible, Fidel Castro, John Wayne, Walter Payton].
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:316)
    at DiscreteMathProject.MainClass.main(MainClass.java: 831)




  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    No, it's not that it'll accept class sizes of only 15. This is proof.
     

    The class: Sleeping 101 is at 2:00 p.m.and has 15 students. The students in it are: [Susan B. Anthony, Martin Luther, Peter Parker, Darth Vader, Walter Payton, Abraham Lincoln, Paul Adcock, James Bond, George Herman (Babe) Ruth, Albert Einstein, Joe McDonald, Emperor Palpatine, Fidel Castro, John Wayne, Benjamin Franklin].
    The class: Partying 202 is at 9:30 a.m.and has 21 students. The students in it are: [Sherlock Holmes, Susan B. Anthony, Martin Luther, Peter Parker, Darth Vader, Walter Payton, Abraham Lincoln, Paul Adcock, James Bond, Albert Einstein, George Herman (Babe) Ruth, Spongebob Squarepants, Joe McDonald, Luke Skywalker, John Wayne].
    The class: Discrete Math is at 9:30 p.m.and has 17 students. The students in it are: [Susan B. Anthony, Sherlock Holmes, Martin Luther, Julius Ceasar, Darth Vader, Abraham Lincoln, Paul Adcock, James Bond, George Herman (Babe) Ruth, Albert Einstein, Spongebob Squarepants, Joe McDonald, Emperor Palpatine, Luke Skywalker, John Wayne].
    The class: Rocket Science is at 9:30 a.m.and has 15 students. The students in it are: [Susan B. Anthony, Sherlock Holmes, Martin Luther, Julius Ceasar, Peter Parker, Walter Payton, Darth Vader, Paul Adcock, James Bond, George Herman (Babe) Ruth, Albert Einstein, Spongebob Squarepants, Joe McDonald, Judas Iscariot, Emperor Palpatine].
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at DiscreteMathProject.Class.getStudentsInClass(Class .java:121)
    at DiscreteMathProject.Class.toString(Class.java:112)
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:265)
    at DiscreteMathProject.MainClass.main(MainClass.java: 831)




  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    What's wrong with this:

     public String getStudentsInClass()
        {
     
            for (int i = 0; i < getSize(); i++)
            {
                aList2.add(aList.get(i).getName()); // it points to here as the problem
            }
     
            String str = aList2.toString();
            return str;
        }

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: ArrayList troubles

    What is the error message?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

     

    The class: Sleeping 101 is at 8:00 a.m.and has 15 students. The students in it are: [Adolf Hitler, Paul Adcock, Albert Einstein, Martin Luther, Darth Vader, John Wayne, Julius Ceasar, Spongebob Squarepants, Walter Payton, George Herman (Babe) Ruth, Emperor Palpatine, Peter Parker, Joe McDonald, Susan B. Anthony, Fidel Castro].


     

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at DiscreteMathProject.Class.getStudentsInClass(Class .java:121)
    at DiscreteMathProject.Class.toString(Class.java:112)
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:228)
    at DiscreteMathProject.MainClass.main(MainClass.java: 831)



  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    Ok, these are my class sizes, in order.

    The ones that did print out right are also in order in the classes array.
     

    20
    18
    15
    17
    15
    16
    15
    19
    24
    16
    15
    15
    25
    15
    21
    15
    15
    17
    15
    15
    15
    15
    15
    25
    16
    15
    15
    15
    15
    22
    23
    18
    15
    15
    15
    The class: Sleeping 101 is at 12:30 p.m.and has 16 students. The students in it are: [Fidel Castro, Emperor Palpatine, Joseph Stalin, Julius Ceasar, Spongebob Squarepants, Susan B. Anthony, Abraham Lincoln, Peter Parker, Adolf Hitler, James Bond, John Wayne, Sherlock Holmes, Walter Payton, Jabba The Hutt, Albert Einstein].
    The class: Partying 202 is at 5:00 p.m.and has 15 students. The students in it are: [Joe McDonald, Martin Luther, Joseph Stalin, Julius Ceasar, Spongebob Squarepants, Susan B. Anthony, Abraham Lincoln, Peter Parker, James Bond, Paul Adcock, John Wayne, George Herman (Babe) Ruth, Sherlock Holmes, Albert Einstein, Darth Vader].




    Why aren't the sizes matching the number of students in the brackets?

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    I think that the number of students in the brackets, the number of students the class says there are in the class that the toString() generates, and the class size are all not matching.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

     

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:213)
    at DiscreteMathProject.MainClass.main(MainClass.java: 836)
    Sleeping 101
    19
    15
    15
    25
    15
    15
    24
    16
    15
    15
    16
    15
    15
    25
    16
    15
    20
    15
    15
    15
    19
    15
    15
    24
    15
    20
    15
    15
    21
    15
    25
    16
    15
    17
    15



  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

     

    The size of class Sleeping 101 is 15
    The size of class Partying 202 is 24
    The size of class Discrete Math is 24
    The size of class Rocket Science is 15
    The size of class Introduction to Data Structures is 15
    The size of class Skipping 303 is 15
    The size of class Creative Writing is 15
    The size of class Introduction to Technical Communication is 15
    The size of class Easy Class is 15
    The size of class Surviving College is 17
    The size of class How To Outsmart the Profs is 15
    The size of class Astronomy is 25
    The size of class Thermodynamics is 15
    The size of class Long-winded Lectures 404 is 18
    The size of class System Design is 15
    The size of class Physics is 24
    The size of class Chemistry is 15
    The size of class Tutoring is 15
    The size of class Politics 204 is 15
    The size of class Very Hard Class is 18
    The size of class Introduction to Java Programming is 23
    The size of class Biology is 15
    The size of class Physical Education is 15
    The size of class Databases is 15
    The size of class World History is 15
    The size of class Philosophy is 16
    The size of class Daydreaming 404 is 23
    The size of class Eating 208 is 18
    The size of class Kung Fu Fighting is 18
    The size of class Religion is 25
    The size of class Film Making is 20
    The size of class How to Understand Women(Sorta) 467 is 15
    The size of class How to Understand Men(Sorta) 468 is 15
    The size of class TV Studio 303 is 15
    The size of class Architecture is 15
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at DiscreteMathProject.Class.getStudentsInClass(Class .java:122)
    at DiscreteMathProject.Class.toString(Class.java:113)
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:215)
    at DiscreteMathProject.MainClass.main(MainClass.java: 836)




  11. #11
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: ArrayList troubles

    Well you have a getSize method as part of your "Class" class (should really pick a better name). This method returns the class variable size. You also have a list of students, that has its own size, as all collections do. Why don't you just use this size. Obviously somehwere the class variable size is getting incremented even though the list isn't growing.

    This is why you are getting the out of bounds exception. The list has 15 students in it (from 0 - 14), and you are trying to get a student at index 15 (which is out of bounds).

    Alternatively, you could iterate over the list by doing the following:

    for(Student myStudent : aList){
    //Code here
    }

  12. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 18th, 2010)

  13. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    How would I use the for each loop?

    I'm not very familiar with those.

    Where would it go?

  14. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: ArrayList troubles

    Quote Originally Posted by DavidFongs View Post
    Well you have a getSize method as part of your "Class" class (should really pick a better name). This method returns the class variable size. You also have a list of students, that has its own size, as all collections do. Why don't you just use this size. Obviously somehwere the class variable size is getting incremented even though the list isn't growing.

    This is why you are getting the out of bounds exception. The list has 15 students in it (from 0 - 14), and you are trying to get a student at index 15 (which is out of bounds).

    Alternatively, you could iterate over the list by doing the following:
    Ok, what is that doing?

  15. #14
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: ArrayList troubles

    Quote Originally Posted by javapenguin View Post
    How would I use the for each loop?

    I'm not very familiar with those.

    Where would it go?
    Instead of accessing the student object by using aList.get(i), you already have a reference to it as myStudent.

    but you could also fix your code, by leaving it the way it is, and instead changing the for loop to

    for(int i = 0; i < aList.size(); i++){
         //code here
    }

    Your main problem is that you have a size variable as part of the "Class" class, and it is unneeded. The size is always the number of students in the list. You don't need another variable to keep track of the size (especially since you are doing it incorrectly somewhere).

  16. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 18th, 2010)

  17. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: ArrayList troubles

    Quote Originally Posted by DavidFongs View Post
    Instead of accessing the student object by using aList.get(i), you already have a reference to it as myStudent.

    but you could also fix your code, by leaving it the way it is, and instead changing the for loop to

    for(int i = 0; i < aList.size(); i++){
         //code here
    }

    Your main problem is that you have a size variable as part of the "Class" class, and it is unneeded. The size is always the number of students in the list. You don't need another variable to keep track of the size (especially since you are doing it incorrectly somewhere).
    Where do I change this?

    In my MainClass or in Class?

    If the Class class, inside what method?

  18. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    If I change it to that in getStudentsInClass I get a Null Pointer Exception

  19. #17
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: ArrayList troubles

    For some reason you didn't post the error, but the only way aList.size() can throw a null pointer exception, is if you didn't instantiate a new ArrayList and assign it to aList

  20. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 18th, 2010)

  21. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

      public String getStudentsInClass()
        {
     
            for (int i = 0; i < aList.size(); i++)
            {
                aList2.add(aList.get(i).getName());
            }
     
     
     
     
            String str = aList2.toString();
            return str;
        }

     

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:285)
    at DiscreteMathProject.MainClass.main(MainClass.java: 836)



  22. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

     

    Exception in thread "main" java.lang.NullPointerException
    at DiscreteMathProject.Class.getStudentsInClass(Class .java:122)
    at DiscreteMathProject.Class.toString(Class.java:113)
    at DiscreteMathProject.MainClass.<init>(MainClass.jav a:215)
    at DiscreteMathProject.MainClass.main(MainClass.java: 836)



  23. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: ArrayList troubles

    Am updating classes.

    Show me where it's going wrong and how to fix it?
    package DiscreteMathProject;
     
    import java.util.ArrayList;
    import java.util.Random;
     
    /**
     * @author pradcoc
     *
     */
    public class Class {
     
        private Student[] students;
        private String[] times;
        private String name;
        private String time;
        private int size;
        private Random r,r2;
        private Random[] random;
        private ArrayList<Student> aList;
        private ArrayList<String> aList2;
     
        public Class(String name)
        {
     
            name = this.name;
            times = new String[10];
            times[0] = "8:00 a.m.";
            times[1] = "9:30 a.m.";
            times[2] = "11:00 a.m.";
            times[3] = "12:30 p.m.";
            times[4] = "2:00 p.m.";
            times[5] = "3:30 p.m.";
            times[6] = "5:00 p.m.";
            times[7] = "6:30 p.m.";
            times[8] = "8:00 p.m.";
            times[9] = "9:30 p.m.";
            aList = new ArrayList<Student>();
       aList2 = new ArrayList<String>();
          setName(name);
     
     
        }
        public String getTime()
        {
            r = new Random();
            int x = r.nextInt(10);
            time= times[x];
            return time;
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String name2)
        {
            name = name2;
        }
     
        public void addStudents(Student s)
        {
     
            aList.add(s);
     
        }
     
    public String  getAdjacentClasses(Class[] classes)
    {
    ArrayList<String> penguinList = new ArrayList<String>();
     
    for (int i = 0; i < classes.length; i++)
    {
     
    // how do I get it so that it checks the getArrayList2() method of each class and checks to see if
    // the get method, for any index, returns true, i.e., they have a Student in common
    // I'm baffled how to do it without it throwing some exception somewhere or making a glitch somewhere
     
    }
    String str = "Help";
    return str;
    }
     
        public ArrayList<Student> getArrayList()
        {
            return aList;
        }
     
        public int getSize()
        {
        r2 = new Random();
     
        int x = r2.nextInt(26);
        if (x < 15)
        {
            x = 15;
            size = x;
            return size;
        }
     
     
        else
        {
        size = x;
        return size;
        }
     
        }
     
        public String toString()
        {
            String str = "The class: " + getName() + " is at " + getTime() + "and has " + getSize() + " students." +
            " The students in it are: " + getStudentsInClass() + "." ;
            return str;
        }
     
        public String getStudentsInClass()
        {
     
            for (int i = 0; i < getSize(); i++)
            {
                aList2.add(aList.get(i).getName());
            }
     
     
     
     
            String str = aList2.toString();
            return str;
        }
     
    public ArrayList<String> getStudentsInClass2()
    {
    return aList2;
    }
    }

    package DiscreteMathProject;
     
    import java.util.ArrayList;
     
    /**
     * @author pradcoc
     *
     */
    public class Student {
     
        private String name;
        private int numOfClasses;
        private ArrayList<Class> aList;
        private ArrayList<String> aList2;
     
        public Student(String name)
        {
     
            name = this.name;
             setName(name);
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String name2)
        {
            name = name2;
        }
     
       public void addClasses(Class c)
    {
    aList.add(c);
    }
     
    public String getClasses()
    {
    for (int i = 0; i < aList.size(); i++)
            {
                aList2.add(aList.get(i).getName());
            }
     
            String str = aList2.toString();
            return str;
     
    }
     
    public int getNumOfClasses()
    {
    numOfClasses = aList2.size();
    return numOfClasses;
    }
     
    public String toString()
    {
    String str = "The student's name is: " + getName() + ".  The student is in " + getNumOfClasses() + " classes.  The classes are " + getClasses() + ".";
    return str;
    }
     
    }

    package DiscreteMathProject;
     
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;
    import java.util.HashSet;
     
    public class MainClass {
     
        private String[] studentNames;
        private Student[] students;
        private Class[] classes;
        private String[] classNames;
        private ArrayList<Student> aList2;
        private Random[] random;
        PrintWriter outFile;
        public MainClass() throws FileNotFoundException
        {
            outFile = new PrintWriter("classes.txt");
            classes = new Class[35];
            studentNames = new String[500];
            students = new Student[500];
            studentNames[0] = "Joe McDonald";
            studentNames[1] = "Paul Adcock";
            studentNames[2] = "Darth Vader";
            studentNames[3] = "Peter Parker";
            studentNames[4] = "Abraham Lincoln";
            studentNames[5] = "Albert Einstein";
            studentNames[6] = "Spongebob Squarepants";
            studentNames[7] = "James Bond";
            studentNames[8] = "Sherlock Holmes";
            studentNames[9] = "John Wayne";
            studentNames[10] = "Walter Payton";
            studentNames[11] = "George Herman (Babe) Ruth";
            studentNames[12] = "Susan B. Anthony";
            studentNames[13] = "Martin Luther";
            studentNames[14] = "Julius Ceasar";
            studentNames[15] = "Adolf Hitler";
            studentNames[16] = "Joseph Stalin";
            studentNames[17] = "Fidel Castro";
            studentNames[18] = "Ivan The Terrible";
            studentNames[19] = "Jabba The Hutt";
            studentNames[20] = "Luke Skywalker";
            studentNames[21] = "Emperor Palpatine";
            studentNames[22] = "Thomas Edison";
            studentNames[23] = "Benjamin Franklin";
            studentNames[24] = "Judas Iscariot";
            studentNames[25] = "King David";
            studentNames[26] = "Blaise Pascal";
            studentNames[27] = "John Watson";
            studentNames[28] = "James Watt";
            studentNames[29] = "Henry Ford";
            studentNames[30] = "Michael Jackson";
            studentNames[31] = "Mohatma Ghandi";
            studentNames[32] = "Emperor Mao";
            studentNames[33] = "Robin of Locksley (Robin Hood)";
            studentNames[34] = "King Richard The Lionhearted";
            studentNames[35] = "Harry Potter";
            studentNames[36] = "John Smith";
            studentNames[37] = "King John";
            studentNames[38] = "William The Conqueror";
            studentNames[39] = "Bloody Mary";
            studentNames[40] = "Johnny Cash";
            studentNames[41] = "Sargon of Akkad";
            studentNames[42] = "Prince Absalom";
            studentNames[43] = "Eustace Clarence";
            studentNames[44] = "Ronald Weasley";
            studentNames[45] = "Rosa Parks";
            studentNames[46] = "Sandra Day O'Connor";
            studentNames[47] = "Queen Lucy Pevensie";
            studentNames[48] = "John The Apostle";
            studentNames[49] = "Kevin McAlister";
            studentNames[50] = "Alex Pruitt";
            studentNames[51] = "Guy Fawkes";
            studentNames[52] = "Queen Heshepshut";
            studentNames[53] = "King Ramases";
            studentNames[54] = "King Xerxes of Persia";
            studentNames[55] = "Nathan Hale";
            studentNames[56] = "Patrick Henry";
            studentNames[57] = "Jacob Marley";
            studentNames[58] = "Ebenezer Scrooge";
            studentNames[59] = "Saint Augustine";
            studentNames[60] = "John Lennon";
            studentNames[61] = "Forrest Gump";
            studentNames[62] = "Jenny Gump";
            studentNames[63] = "Luitenant Dan";
            studentNames[64] = "John Q. Public";
            studentNames[65] = "Bill Gates";
            studentNames[66] = "Harriet Tubman";
            studentNames[67] = "Sojourner Truth";
            studentNames[68] = "Malcom Little(Malcom X)";
            studentNames[69] = "Leonardo D. Caprio";
            studentNames[70] = "Jack Philps";
            studentNames[71] = "Paul Revere";
            studentNames[72] = "Becky Thatcher";
            studentNames[73] = "Tom Sawyer";
            studentNames[74] = "Samual Clemmons(Mark Twain)";
            studentNames[75] = "George Washington Carver";
            studentNames[76] = "Paul Bunyan";
            studentNames[77] = "Huck Finn";
            studentNames[78] = "Picos Bill";
            studentNames[79] = "Mark Anthony";
            studentNames[80] = "Queen Cleopatra";
            studentNames[81] = "Anne Frank";
            studentNames[82] = "Eli Wesel";
            studentNames[83] = "Eddie Brock";
            studentNames[84] = "Thorin Oakenshield";
            studentNames[85] = "Bilbo Bagins";
            studentNames[86] = "Frodo Baggins";
            studentNames[87] = "Sam Gamagee";
            studentNames[88] = "Gandalf the Gray";
            studentNames[89] = "Sauraman the Wise";
            studentNames[90] = "King George III";
            studentNames[91] = "William Wilberforce";
            studentNames[92] = "Merry Brandybuck";
            studentNames[93] = "Martin Luther King Jr.";
            studentNames[94] = "Jackie Robinson";
            studentNames[95] = "Sherwim Williams";
            studentNames[96] = "John Menard";
            studentNames[97] = "Chrisopher Columbus";
            studentNames[98] = "Genghis Khan";
            studentNames[99] = "Atilla The Hun";
            studentNames[100] = "Al Capone";
            studentNames[101] = "Napolean Bonepart";
            studentNames[102] = "Marie Antionette";
            studentNames[103] = "King Louis XVII";
            studentNames[104] = "Napolean Blownapart";
            studentNames[105] = "Yesser Arafat";
            studentNames[106] = "Al Gore";
            studentNames[107] = "Benedict Arnold";
            studentNames[108] = "Tim Allen";
            studentNames[109] = "George W. Bush";
            for (int x = 110; x < 500; x++)
            {
                studentNames[x] = "Student " + x;
            }
     
            for (int y = 0; y < 500; y++)
            {
                students[y] = new Student(studentNames[y]);
                students[y].setName(studentNames[y]);
     
            }
     
            classNames = new String[35];
     
            classNames[0] = "Sleeping 101";
            classNames[1] = "Partying 202";
            classNames[2] = "Discrete Math";
            classNames[3] = "Rocket Science";
            classNames[4] = "Introduction to Data Structures";
            classNames[5] = "Skipping 303";
            classNames[6] = "Creative Writing";
            classNames[7] = "Introduction to Technical Communication";
            classNames[8] = "Easy Class";
            classNames[9] = "Surviving College";
            classNames[10] = "How To Outsmart the Profs";
            classNames[11] = "Astronomy";
            classNames[12] = "Thermodynamics";
            classNames[13] = "Long-winded Lectures 404";
            classNames[14] = "System Design";
            classNames[15] = "Physics";
            classNames[16] = "Chemistry";
            classNames[17] = "Tutoring";
            classNames[18] = "Politics 204";
            classNames[19] = "Very Hard Class";
            classNames[20] = "Introduction to Java Programming";
            classNames[21] = "Biology";
            classNames[22] = "Physical Education";
            classNames[23] = "Databases";
            classNames[24] = "World History";
            classNames[25] = "Philosophy";
            classNames[26] = "Daydreaming 404";
            classNames[27] = "Eating 208";
            classNames[28] = "Kung Fu Fighting";
            classNames[29] = "Religion";
            classNames[30] = "Film Making";
            classNames[31] = "How to Understand Women(Sorta) 467";
            classNames[32] = "How to Understand Men(Sorta) 468";
            classNames[33] = "TV Studio 303";
            classNames[34] = "Architecture";
     
            for (int z = 0; z < 35; z++)
            {
                classes[z] = new Class(classNames[z]);
                classes[z].setName(classNames[z]);
     
            }
           System.out.println(classes[0].getName());
           for (int x =0; x < 35; x++)
           {
          	 System.out.println("The size of class " + classes[x].getName() + " is " + classes[x].getSize());
           }
     
     
    Random rand1 = new Random();
     Set<Student> mix1 = new HashSet<Student>();
    Student[] st1 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[0].getSize(); i++ ){
    int rN1 = rand1.nextInt(classes[0].getSize());
    while ( mix1.contains(students[rN1]) ){//already chose
    rN1= rand1.nextInt(classes[0].getSize());//get another
    }
    mix1.add(students[rN1]);
    mix1.toArray(st1);
     st1 = mix1.toArray(st1);
     }
     for (int k = 0; k < classes[0].getSize()  ; k++)
    {
      classes[0].addStudents(st1[k]);
    }
    System.out.println(classes[0].toString());
    outFile.println(classes[0].toString());
    Random rand2 = new Random();
     Set<Student> mix2 = new HashSet<Student>();
    Student[] st2 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[1].getSize(); i++ ){
    int rN2 = rand2.nextInt(classes[1].getSize());
    while ( mix2.contains(students[rN2]) ){//already chose
    rN2= rand2.nextInt(classes[1].getSize());//get another
    }
    mix2.add(students[rN2]);
    mix2.toArray(st2);
     st2 = mix2.toArray(st2);
     }
     for (int k = 0; k < classes[1].getSize()  ; k++)
    {
      classes[1].addStudents(st2[k]);
    }
    System.out.println(classes[1].toString());
    outFile.println(classes[1].toString());
    Random rand3 = new Random();
     Set<Student> mix3 = new HashSet<Student>();
    Student[] st3 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[2].getSize(); i++ ){
    int rN3 = rand3.nextInt(classes[2].getSize());
    while ( mix3.contains(students[rN3]) ){//already chose
    rN3= rand3.nextInt(classes[2].getSize());//get another
    }
    mix3.add(students[rN3]);
    mix3.toArray(st3);
     st3 = mix3.toArray(st3);
     }
     for (int k = 0; k < classes[2].getSize()  ; k++)
    {
      classes[2].addStudents(st3[k]);
    }
    System.out.println(classes[2].toString());
    outFile.println(classes[2].toString());
    Random rand4 = new Random();
     Set<Student> mix4 = new HashSet<Student>();
    Student[] st4 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[3].getSize(); i++ ){
    int rN4 = rand4.nextInt(classes[3].getSize());
    while ( mix4.contains(students[rN4]) ){//already chose
    rN4= rand4.nextInt(classes[3].getSize());//get another
    }
    mix4.add(students[rN4]);
    mix4.toArray(st4);
     st4 = mix4.toArray(st4);
     }
     for (int k = 0; k < classes[3].getSize()  ; k++)
    {
      classes[3].addStudents(st4[k]);
    }
    System.out.println(classes[3].toString());
    outFile.println(classes[3].toString());
    Random rand5 = new Random();
     Set<Student> mix5 = new HashSet<Student>();
    Student[] st5 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[4].getSize(); i++ ){
    int rN5 = rand5.nextInt(classes[4].getSize());
    while ( mix5.contains(students[rN5]) ){//already chose
    rN5= rand5.nextInt(classes[4].getSize());//get another
    }
    mix5.add(students[rN5]);
    mix5.toArray(st5);
     st5 = mix5.toArray(st5);
     }
     for (int k = 0; k < classes[4].getSize()  ; k++)
    {
      classes[4].addStudents(st5[k]);
    }
    System.out.println(classes[4].toString());
    outFile.println(classes[4].toString());
    Random rand6 = new Random();
     Set<Student> mix6 = new HashSet<Student>();
    Student[] st6 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[5].getSize(); i++ ){
    int rN6 = rand6.nextInt(classes[5].getSize());
    while ( mix6.contains(students[rN6]) ){//already chose
    rN6= rand6.nextInt(classes[5].getSize());//get another
    }
    mix6.add(students[rN6]);
    mix6.toArray(st6);
     st6 = mix6.toArray(st6);
     }
     for (int k = 0; k < classes[5].getSize()  ; k++)
    {
      classes[5].addStudents(st6[k]);
    }
    System.out.println(classes[5].toString());
    outFile.println(classes[5].toString());
    Random rand7 = new Random();
     Set<Student> mix7 = new HashSet<Student>();
    Student[] st7 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[6].getSize(); i++ ){
    int rN7 = rand7.nextInt(classes[6].getSize());
    while ( mix7.contains(students[rN7]) ){//already chose
    rN7= rand7.nextInt(classes[6].getSize());//get another
    }
    mix7.add(students[rN7]);
    mix7.toArray(st7);
     st7 = mix7.toArray(st7);
     }
     for (int k = 0; k < classes[6].getSize()  ; k++)
    {
      classes[6].addStudents(st7[k]);
    }
    System.out.println(classes[6].toString());
    outFile.println(classes[6].toString());
    Random rand8 = new Random();
     Set<Student> mix8 = new HashSet<Student>();
    Student[] st8 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[7].getSize(); i++ ){
    int rN8 = rand8.nextInt(classes[7].getSize());
    while ( mix8.contains(students[rN8]) ){//already chose
    rN8= rand8.nextInt(classes[7].getSize());//get another
    }
    mix8.add(students[rN8]);
    mix8.toArray(st8);
     st8 = mix8.toArray(st8);
     }
     for (int k = 0; k < classes[7].getSize()  ; k++)
    {
      classes[7].addStudents(st8[k]);
    }
    System.out.println(classes[7].toString());
    outFile.println(classes[7].toString());
    Random rand9 = new Random();
     Set<Student> mix9 = new HashSet<Student>();
    Student[] st9 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[8].getSize(); i++ ){
    int rN9 = rand9.nextInt(classes[8].getSize());
    while ( mix9.contains(students[rN9]) ){//already chose
    rN9= rand9.nextInt(classes[8].getSize());//get another
    }
    mix9.add(students[rN9]);
    mix9.toArray(st9);
     st9 = mix9.toArray(st9);
     }
     for (int k = 0; k < classes[8].getSize()  ; k++)
    {
      classes[8].addStudents(st9[k]);
    }
    System.out.println(classes[8].toString());
    outFile.println(classes[8].toString());
    Random rand10 = new Random();
     Set<Student> mix10 = new HashSet<Student>();
    Student[] st10 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[9].getSize(); i++ ){
    int rN10 = rand10.nextInt(classes[9].getSize());
    while ( mix10.contains(students[rN10]) ){//already chose
    rN10= rand10.nextInt(classes[9].getSize());//get another
    }
    mix10.add(students[rN10]);
    mix10.toArray(st10);
     st10 = mix10.toArray(st10);
     }
     for (int k = 0; k < classes[9].getSize()  ; k++)
    {
      classes[9].addStudents(st10[k]);
    }
    System.out.println(classes[9].toString());
    outFile.println(classes[9].toString());
    Random rand11 = new Random();
     Set<Student> mix11 = new HashSet<Student>();
    Student[] st11 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[10].getSize(); i++ ){
    int rN11 = rand11.nextInt(classes[10].getSize());
    while ( mix11.contains(students[rN11]) ){//already chose
    rN11= rand11.nextInt(classes[10].getSize());//get another
    }
    mix11.add(students[rN11]);
    mix11.toArray(st11);
     st11 = mix11.toArray(st11);
     }
     for (int k = 0; k < classes[10].getSize()  ; k++)
    {
      classes[10].addStudents(st11[k]);
    }
    System.out.println(classes[10].toString());
    outFile.println(classes[10].toString());
    Random rand12 = new Random();
     Set<Student> mix12 = new HashSet<Student>();
    Student[] st12 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[11].getSize(); i++ ){
    int rN12 = rand12.nextInt(classes[11].getSize());
    while ( mix12.contains(students[rN12]) ){//already chose
    rN12= rand12.nextInt(classes[11].getSize());//get another
    }
    mix12.add(students[rN12]);
    mix12.toArray(st12);
     st12 = mix12.toArray(st12);
     }
     for (int k = 0; k < classes[11].getSize()  ; k++)
    {
      classes[11].addStudents(st12[k]);
    }
    System.out.println(classes[11].toString());
    outFile.println(classes[11].toString());
    Random rand13 = new Random();
     Set<Student> mix13 = new HashSet<Student>();
    Student[] st13 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[12].getSize(); i++ ){
    int rN13 = rand13.nextInt(classes[12].getSize());
    while ( mix13.contains(students[rN13]) ){//already chose
    rN13= rand13.nextInt(classes[12].getSize());//get another
    }
    mix13.add(students[rN13]);
    mix13.toArray(st13);
     st13 = mix13.toArray(st13);
     }
     for (int k = 0; k < classes[12].getSize()  ; k++)
    {
      classes[12].addStudents(st13[k]);
    }
    System.out.println(classes[12].toString());
    outFile.println(classes[12].toString());
    Random rand14 = new Random();
     Set<Student> mix14 = new HashSet<Student>();
    Student[] st14 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[13].getSize(); i++ ){
    int rN14 = rand14.nextInt(classes[13].getSize());
    while ( mix14.contains(students[rN14]) ){//already chose
    rN14= rand14.nextInt(classes[13].getSize());//get another
    }
    mix14.add(students[rN14]);
    mix14.toArray(st14);
     st14 = mix14.toArray(st14);
     }
     for (int k = 0; k < classes[13].getSize()  ; k++)
    {
      classes[13].addStudents(st14[k]);
    }
    System.out.println(classes[13].toString());
    outFile.println(classes[13].toString());
    Random rand15 = new Random();
     Set<Student> mix15 = new HashSet<Student>();
    Student[] st15 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[14].getSize(); i++ ){
    int rN15 = rand15.nextInt(classes[14].getSize());
    while ( mix15.contains(students[rN15]) ){//already chose
    rN15= rand15.nextInt(classes[14].getSize());//get another
    }
    mix15.add(students[rN15]);
    mix15.toArray(st15);
     st15 = mix15.toArray(st15);
     }
     for (int k = 0; k < classes[14].getSize()  ; k++)
    {
      classes[14].addStudents(st15[k]);
    }
    System.out.println(classes[14].toString());
    outFile.println(classes[14].toString());
    Random rand16 = new Random();
     Set<Student> mix16 = new HashSet<Student>();
    Student[] st16 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[15].getSize(); i++ ){
    int rN16 = rand16.nextInt(classes[15].getSize());
    while ( mix16.contains(students[rN16]) ){//already chose
    rN16= rand16.nextInt(classes[15].getSize());//get another
    }
    mix16.add(students[rN16]);
    mix16.toArray(st16);
     st16 = mix16.toArray(st16);
     }
     for (int k = 0; k < classes[15].getSize()  ; k++)
    {
      classes[15].addStudents(st16[k]);
    }
    System.out.println(classes[15].toString());
    outFile.println(classes[15].toString());
    Random rand17 = new Random();
     Set<Student> mix17 = new HashSet<Student>();
    Student[] st17 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[16].getSize(); i++ ){
    int rN17 = rand17.nextInt(classes[16].getSize());
    while ( mix17.contains(students[rN17]) ){//already chose
    rN17= rand17.nextInt(classes[16].getSize());//get another
    }
    mix17.add(students[rN17]);
    mix17.toArray(st17);
     st17 = mix17.toArray(st17);
     }
     for (int k = 0; k < classes[16].getSize()  ; k++)
    {
      classes[16].addStudents(st17[k]);
    }
    System.out.println(classes[16].toString());
    outFile.println(classes[16].toString());
    Random rand18 = new Random();
     Set<Student> mix18 = new HashSet<Student>();
    Student[] st18 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[17].getSize(); i++ ){
    int rN18 = rand18.nextInt(classes[17].getSize());
    while ( mix18.contains(students[rN18]) ){//already chose
    rN18= rand18.nextInt(classes[17].getSize());//get another
    }
    mix18.add(students[rN18]);
    mix18.toArray(st18);
     st18 = mix18.toArray(st18);
     }
     for (int k = 0; k < classes[17].getSize()  ; k++)
    {
      classes[17].addStudents(st18[k]);
    }
    System.out.println(classes[17].toString());
    outFile.println(classes[17].toString());
    Random rand19 = new Random();
     Set<Student> mix19 = new HashSet<Student>();
    Student[] st19 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[18].getSize(); i++ ){
    int rN19 = rand19.nextInt(classes[18].getSize());
    while ( mix19.contains(students[rN19]) ){//already chose
    rN19= rand19.nextInt(classes[18].getSize());//get another
    }
    mix19.add(students[rN19]);
    mix19.toArray(st19);
     st19 = mix19.toArray(st19);
     }
     for (int k = 0; k < classes[18].getSize()  ; k++)
    {
      classes[18].addStudents(st19[k]);
    }
    System.out.println(classes[18].toString());
    outFile.println(classes[18].toString());
    Random rand20 = new Random();
     Set<Student> mix20 = new HashSet<Student>();
    Student[] st20 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[19].getSize(); i++ ){
    int rN20 = rand20.nextInt(classes[19].getSize());
    while ( mix20.contains(students[rN20]) ){//already chose
    rN20= rand20.nextInt(classes[19].getSize());//get another
    }
    mix20.add(students[rN20]);
    mix20.toArray(st20);
     st20 = mix20.toArray(st20);
     }
     for (int k = 0; k < classes[19].getSize()  ; k++)
    {
      classes[19].addStudents(st20[k]);
    }
    System.out.println(classes[19].toString());
    outFile.println(classes[19].toString());
    Random rand21 = new Random();
     Set<Student> mix21 = new HashSet<Student>();
    Student[] st21 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[20].getSize(); i++ ){
    int rN21 = rand21.nextInt(classes[20].getSize());
    while ( mix21.contains(students[rN21]) ){//already chose
    rN21= rand21.nextInt(classes[20].getSize());//get another
    }
    mix21.add(students[rN21]);
    mix21.toArray(st21);
     st21 = mix21.toArray(st21);
     }
     for (int k = 0; k < classes[20].getSize()  ; k++)
    {
      classes[20].addStudents(st21[k]);
    }
    System.out.println(classes[20].toString());
    outFile.println(classes[20].toString());
    Random rand22 = new Random();
     Set<Student> mix22 = new HashSet<Student>();
    Student[] st22 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[21].getSize(); i++ ){
    int rN22 = rand22.nextInt(classes[21].getSize());
    while ( mix22.contains(students[rN22]) ){//already chose
    rN22= rand22.nextInt(classes[21].getSize());//get another
    }
    mix22.add(students[rN22]);
    mix22.toArray(st22);
     st22 = mix22.toArray(st22);
     }
     for (int k = 0; k < classes[21].getSize()  ; k++)
    {
      classes[21].addStudents(st22[k]);
    }
    System.out.println(classes[21].toString());
    outFile.println(classes[21].toString());
    Random rand23 = new Random();
     Set<Student> mix23 = new HashSet<Student>();
    Student[] st23 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[22].getSize(); i++ ){
    int rN23 = rand23.nextInt(classes[22].getSize());
    while ( mix23.contains(students[rN23]) ){//already chose
    rN23= rand23.nextInt(classes[22].getSize());//get another
    }
    mix23.add(students[rN23]);
    mix23.toArray(st23);
     st23 = mix23.toArray(st23);
     }
     for (int k = 0; k < classes[22].getSize()  ; k++)
    {
      classes[22].addStudents(st23[k]);
    }
    System.out.println(classes[22].toString());
    outFile.println(classes[22].toString());
    Random rand24 = new Random();
     Set<Student> mix24 = new HashSet<Student>();
    Student[] st24 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[23].getSize(); i++ ){
    int rN24 = rand24.nextInt(classes[23].getSize());
    while ( mix24.contains(students[rN24]) ){//already chose
    rN24= rand24.nextInt(classes[23].getSize());//get another
    }
    mix24.add(students[rN24]);
    mix24.toArray(st24);
     st24 = mix24.toArray(st24);
     }
     for (int k = 0; k < classes[23].getSize()  ; k++)
    {
      classes[23].addStudents(st24[k]);
    }
    System.out.println(classes[23].toString());
    outFile.println(classes[23].toString());
    Random rand25 = new Random();
     Set<Student> mix25 = new HashSet<Student>();
    Student[] st25 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[24].getSize(); i++ ){
    int rN25 = rand25.nextInt(classes[24].getSize());
    while ( mix25.contains(students[rN25]) ){//already chose
    rN25= rand25.nextInt(classes[24].getSize());//get another
    }
    mix25.add(students[rN25]);
    mix25.toArray(st25);
     st25 = mix25.toArray(st25);
     }
     for (int k = 0; k < classes[24].getSize()  ; k++)
    {
      classes[24].addStudents(st25[k]);
    }
    System.out.println(classes[24].toString());
    outFile.println(classes[24].toString());
    Random rand26 = new Random();
     Set<Student> mix26 = new HashSet<Student>();
    Student[] st26 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[25].getSize(); i++ ){
    int rN26 = rand26.nextInt(classes[25].getSize());
    while ( mix26.contains(students[rN26]) ){//already chose
    rN26= rand26.nextInt(classes[25].getSize());//get another
    }
    mix26.add(students[rN26]);
    mix26.toArray(st26);
     st26 = mix26.toArray(st26);
     }
     for (int k = 0; k < classes[25].getSize()  ; k++)
    {
      classes[25].addStudents(st26[k]);
    }
    System.out.println(classes[25].toString());
    outFile.println(classes[25].toString());
    Random rand27 = new Random();
     Set<Student> mix27 = new HashSet<Student>();
    Student[] st27 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[26].getSize(); i++ ){
    int rN27 = rand27.nextInt(classes[26].getSize());
    while ( mix27.contains(students[rN27]) ){//already chose
    rN27= rand27.nextInt(classes[26].getSize());//get another
    }
    mix27.add(students[rN27]);
    mix27.toArray(st27);
     st27 = mix27.toArray(st27);
     }
     for (int k = 0; k < classes[26].getSize()  ; k++)
    {
      classes[26].addStudents(st27[k]);
    }
    System.out.println(classes[26].toString());
    outFile.println(classes[26].toString());
    Random rand28 = new Random();
     Set<Student> mix28 = new HashSet<Student>();
    Student[] st28 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[27].getSize(); i++ ){
    int rN28 = rand28.nextInt(classes[27].getSize());
    while ( mix28.contains(students[rN28]) ){//already chose
    rN28= rand28.nextInt(classes[27].getSize());//get another
    }
    mix28.add(students[rN28]);
    mix28.toArray(st28);
     st28 = mix28.toArray(st28);
     }
     for (int k = 0; k < classes[27].getSize()  ; k++)
    {
      classes[27].addStudents(st28[k]);
    }
    System.out.println(classes[27].toString());
    outFile.println(classes[27].toString());
    Random rand29 = new Random();
     Set<Student> mix29 = new HashSet<Student>();
    Student[] st29 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[28].getSize(); i++ ){
    int rN29 = rand29.nextInt(classes[28].getSize());
    while ( mix29.contains(students[rN29]) ){//already chose
    rN29= rand29.nextInt(classes[28].getSize());//get another
    }
    mix29.add(students[rN29]);
    mix29.toArray(st29);
     st29 = mix29.toArray(st29);
     }
     for (int k = 0; k < classes[28].getSize()  ; k++)
    {
      classes[28].addStudents(st29[k]);
    }
    System.out.println(classes[28].toString());
    outFile.println(classes[28].toString());
    Random rand30 = new Random();
     Set<Student> mix30 = new HashSet<Student>();
    Student[] st30 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[29].getSize(); i++ ){
    int rN30 = rand30.nextInt(classes[29].getSize());
    while ( mix30.contains(students[rN30]) ){//already chose
    rN30= rand30.nextInt(classes[29].getSize());//get another
    }
    mix30.add(students[rN30]);
    mix30.toArray(st30);
     st30 = mix30.toArray(st30);
     }
     for (int k = 0; k < classes[29].getSize()  ; k++)
    {
      classes[29].addStudents(st30[k]);
    }
    System.out.println(classes[29].toString());
    outFile.println(classes[29].toString());
    Random rand31 = new Random();
     Set<Student> mix31 = new HashSet<Student>();
    Student[] st31 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[30].getSize(); i++ ){
    int rN31 = rand31.nextInt(classes[30].getSize());
    while ( mix31.contains(students[rN31]) ){//already chose
    rN31= rand31.nextInt(classes[30].getSize());//get another
    }
    mix31.add(students[rN31]);
    mix31.toArray(st31);
     st31 = mix31.toArray(st31);
     }
     for (int k = 0; k < classes[30].getSize()  ; k++)
    {
      classes[30].addStudents(st31[k]);
    }
    System.out.println(classes[30].toString());
    outFile.println(classes[30].toString());
    Random rand32 = new Random();
     Set<Student> mix32 = new HashSet<Student>();
    Student[] st32 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[31].getSize(); i++ ){
    int rN32 = rand32.nextInt(classes[31].getSize());
    while ( mix32.contains(students[rN32]) ){//already chose
    rN32= rand32.nextInt(classes[31].getSize());//get another
    }
    mix32.add(students[rN32]);
    mix32.toArray(st32);
     st32 = mix32.toArray(st32);
     }
     for (int k = 0; k < classes[31].getSize()  ; k++)
    {
      classes[31].addStudents(st32[k]);
    }
    System.out.println(classes[31].toString());
    outFile.println(classes[31].toString());
    Random rand33 = new Random();
     Set<Student> mix33 = new HashSet<Student>();
    Student[] st33 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[32].getSize(); i++ ){
    int rN33 = rand33.nextInt(classes[32].getSize());
    while ( mix33.contains(students[rN33]) ){//already chose
    rN33= rand33.nextInt(classes[32].getSize());//get another
    }
    mix33.add(students[rN33]);
    mix33.toArray(st33);
     st33 = mix33.toArray(st33);
     }
     for (int k = 0; k < classes[32].getSize()  ; k++)
    {
      classes[32].addStudents(st33[k]);
    }
    System.out.println(classes[32].toString());
    outFile.println(classes[32].toString());
    Random rand34 = new Random();
     Set<Student> mix34 = new HashSet<Student>();
    Student[] st34 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[33].getSize(); i++ ){
    int rN34 = rand34.nextInt(classes[33].getSize());
    while ( mix34.contains(students[rN34]) ){//already chose
    rN34= rand34.nextInt(classes[33].getSize());//get another
    }
    mix34.add(students[rN34]);
    mix34.toArray(st34);
     st34 = mix34.toArray(st34);
     }
     for (int k = 0; k < classes[33].getSize()  ; k++)
    {
      classes[33].addStudents(st34[k]);
    }
    System.out.println(classes[33].toString());
    outFile.println(classes[33].toString());
    Random rand35 = new Random();
     Set<Student> mix35 = new HashSet<Student>();
    Student[] st35 = new Student[classes[0].getSize()];
      for ( int i = 0; i < classes[34].getSize(); i++ ){
    int rN35 = rand35.nextInt(classes[34].getSize());
    while ( mix35.contains(students[rN35]) ){//already chose
    rN35= rand35.nextInt(classes[34].getSize());//get another
    }
    mix35.add(students[rN35]);
    mix35.toArray(st35);
     st35 = mix35.toArray(st35);
     }
     for (int k = 0; k < classes[34].getSize()  ; k++)
    {
      classes[34].addStudents(st35[k]);
    }
    System.out.println(classes[34].toString());
    outFile.println(classes[34].toString());
    outFile.close();
     
     
        }
     
        public static void main(String[] args) throws FileNotFoundException
        {
            MainClass mainC = new MainClass();
        }
    }

  24. #21
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: ArrayList troubles

    Ok, well the change I suggested works....

    There are other indexing problems, and the reason that change sometimes is throwing a null pointer exception is because aList is being populated with null Students

    The code you wrote is completely uncommented, and I don't have the time to look over it and figure out what you are trying to do.

  25. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 20th, 2010)

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. [SOLVED] ArrayList Troubles.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2010, 10:19 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 01:26 PM
  5. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM