I'm trying to do some homework for my Java class, but I'm getting this OutOfMemoryError that makes absolutely no sense at all to me. I don't see what's wrong with it. Here's the code:
public static void insertStudent(ArrayList<Student> classroom, String oldName, String newName, int qz1, int qz2, int qz3, int qz4, int qz5)
{
for (int i = 0; i < classroom.size(); i++)
{
Student student = classroom.get(i);
if (student != null)
{
if (student.getName().equals(oldName))
classroom.add(i, new Student(newName, qz1, qz2, qz3, qz4, qz5));
}
}
}
The error occurs when I try to add a new Student object into the classroom ArrayList. The Student class has all of the proper constructors. Does anyone have any idea why this is happening?
How are you originally populating that list? The way you're checking stuff to insert a new student seems like you're populating an ArrayList with a bunch of nulls. I'm also curious why you're only creating the student in this method, and not outside it, which could help condense the parameters list.
I'm also wondering exactly what this method does. As far as I can tell, it adds a new Student into the list at a point when it finds a student whose name is whatever oldName points to.
I'm also wondering exactly what this method does. As far as I can tell, it adds a new Student into the list at a point when it finds a student whose name is whatever oldName points to.
It checks if the strings are equal, not that the references are equal. Though I am equally confused as to why there are "old" and "new" names involved, since the method is called "insertStudent" not "RenameStudent".
That's what I understood it to do, I just worded my response poorly.
It's not even updating a student, so far as I can tell. If my knowledge of Java's ArrayLists is accurate, it's just going to add "Jane" in a slot after the first occurrence of "Jane" it finds in the list. Even then, it only adds the student to the list if there is someone in the list with the oldName.
It seems to me that the OutOfMemory error is being caused by something else, and this is just where an invocation runs into that.
Sorry, I haven't looked back at this until now, mainly because I sort of solved the problem. oldName is the name of the student that I'm inserting the new one before. The ArrayList is initially populated using the add() method for each initial element.
I was able to get it working by assigning the desired "i" value to a variable outside of the loop, and then adding the new student to the ArrayList outside as well. However, I really don't see why the way I had it before wouldn't work.
From what I can tell, and I'm not sure how the logic is handled inside Java's For loops, you may have been getting stuck inside an infinite loop. You never break out of the loop after you reach your point, and you're actually growing the list inside the loop. Assuming that you're adding the new Student before what you're looking for, then your new student becomes the current index you're at. As such, your loop recalculates the length of the loop, finds it can keep going, and the very next index is again what you're looking for. This mean you again add a new Student (the same one you just added!) and the process continues until you use up all the memory!
I assume you're supposed to be keeping the list sorted? There are better ways of doing this. I would highly recommend looking into different sorting algorithms, such as insertion sort, or quick sort. They're basics of computer science.
A better of way of accomplishing what you're trying to do would be recursion. A similar method using recursion would look like:
I'm trying to do some homework for my Java class, but I'm getting this OutOfMemoryError that makes absolutely no sense at all to me. I don't see what's wrong with it. Here's the code:
public static void insertStudent(ArrayList<Student> classroom, String oldName, String newName, int qz1, int qz2, int qz3, int qz4, int qz5)
{
for (int i = 0; i < classroom.size(); i++)
{
Student student = classroom.get(i);
if (student != null)
{
if (student.getName().equals(oldName))
classroom.add(i, new Student(newName, qz1, qz2, qz3, qz4, qz5));
}
}
}
The error occurs when I try to add a new Student object into the classroom ArrayList. The Student class has all of the proper constructors. Does anyone have any idea why this is happening?
How are you originally populating that list? The way you're checking stuff to insert a new student seems like you're populating an ArrayList with a bunch of nulls. I'm also curious why you're only creating the student in this method, and not outside it, which could help condense the parameters list.
I'm also wondering exactly what this method does. As far as I can tell, it adds a new Student into the list at a point when it finds a student whose name is whatever oldName points to.
"Programmers never repeat themselves. They loop."
It checks if the strings are equal, not that the references are equal. Though I am equally confused as to why there are "old" and "new" names involved, since the method is called "insertStudent" not "RenameStudent".
That's what I understood it to do, I just worded my response poorly.
It's not even updating a student, so far as I can tell. If my knowledge of Java's ArrayLists is accurate, it's just going to add "Jane" in a slot after the first occurrence of "Jane" it finds in the list. Even then, it only adds the student to the list if there is someone in the list with the oldName.
It seems to me that the OutOfMemory error is being caused by something else, and this is just where an invocation runs into that.
"Programmers never repeat themselves. They loop."
Sorry, I haven't looked back at this until now, mainly because I sort of solved the problem. oldName is the name of the student that I'm inserting the new one before. The ArrayList is initially populated using the add() method for each initial element.
I was able to get it working by assigning the desired "i" value to a variable outside of the loop, and then adding the new student to the ArrayList outside as well. However, I really don't see why the way I had it before wouldn't work.
From what I can tell, and I'm not sure how the logic is handled inside Java's For loops, you may have been getting stuck inside an infinite loop. You never break out of the loop after you reach your point, and you're actually growing the list inside the loop. Assuming that you're adding the new Student before what you're looking for, then your new student becomes the current index you're at. As such, your loop recalculates the length of the loop, finds it can keep going, and the very next index is again what you're looking for. This mean you again add a new Student (the same one you just added!) and the process continues until you use up all the memory!
I assume you're supposed to be keeping the list sorted? There are better ways of doing this. I would highly recommend looking into different sorting algorithms, such as insertion sort, or quick sort. They're basics of computer science.
A better of way of accomplishing what you're trying to do would be recursion. A similar method using recursion would look like:
"Programmers never repeat themselves. They loop."