• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increasing an Array automatically  RSS feed

 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

So I am currently working my way through a book and doing the exercises to improve my skills. I am stuck here the question in the book says:

The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it

I am using a course class and here is the code for the class

As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

Thanks for reading,

Kind Regards
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

Actually you do not need to increase the array every time you add a new student but this is just an implementation detail.

If you determine that you need a bigger array create a new bigger array.
Then copy all items from old array to new array.
Then replace students reference to point to newly created array.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

You don't need to do that - or at least, certainly not for the first 100 "add"s. What you need to do is to make sure that when you're about to run out of room, you can increase the size of the array; and for that, you'll have to decide how much you want to increase it by (and I'd say that increasing by 1 is not a good choice, since you'll end up increasing it for every add after the first 100, which could be expensive).

But first and foremost, you need to understand WHAT you need to do in order to increase the size of an array.

So, take ONE example to start with: You need to increase your array from 100 to 110 elements.

Describe the exact steps required to do THAT (here if you like). And don't forget that your existing array will already have items in it.

Also: Try to avoid writing Java code when you describe it - ie, describe WHAT you need to do, not HOW you're going to do it in Java. If you write a good one, you should be able to translate it into pretty much any language you like, because the "what" will be the same for them all.

HIH

Winston
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

If i was to increase it from 100 to 110 I would just change it manually. Why can't I just do this?



Obviously this isn't working but I can't understand why. It should just call the method and increase each time someone is added. It's throwing me an out of bounds exception.

So is the solution to make another array to handle overflow and create a method that will copy content from one array to another?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that code compiles at all, then those are not statements. They are declarations of fields, one with an initialisation. They are executed after the class has been instantiated. You can read the exact order in the Java Language Specification, but it is by no means easy to understand. Even if you don't understand it, that doesn't matter; the important thing is, that code can only be executed once. I suggest you want an addStudent() method. That method tests whether the array is full; if so it calls an enlargeArray() method. Give enlargeArray private access. That way you will never overflow. You can create very large arrays but will probably run out of memory before you reach the maximum potential array capacity.
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a addStudent() method which is:


So how would I test if the array is full in there? Would I use .equals?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a size for the array and you have a variable which counts how many students you have. You simply need to check whether they are different or the same.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:
So how would I test if the array is full in there? Would I use .equals?


The array has a length field. And you have a numberOfStudents variable. Can you just compare them?

Henry
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I tried this but it gives me an out of bound exception.

 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so the question states that I should improve it to automatically increase the array size by creating a new larger array and copying into the contents into it. So I did this



But my problem here is how do I implement this and this isn't really automatic but I think this is what the question is suggesting I do.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:Yeah I tried this but it gives me an out of bound exception.



What is the value returned by the getNumberOfStudents() method? Is it by chance negative?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:
But my problem here is how do I implement this and this isn't really automatic but I think this is what the question is suggesting I do.


To make it automatic, you need to know (1) the current number of students (which you do), (2) the current array size (which you do), and (3) an algorithm to check when you are near the maximum, and increase it by a certain size when it happens (which you seem to be ignoring our suggestions).

Henry
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I wasn't ignoring I was just trying to get it to work. No it was returning 3 in the test class. I did this to handle the overflow but surely there is a better way



As you can see I now amended the addStudent() method to copy all the values to the bigger array. I used +50 on the bigStudents() array just to be on the safe side.

(note that I set the original arrays size to 2 so that I could test inputting five objects)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:Ok so the question states that I should improve it to automatically increase the array size by creating a new larger array and copying into the contents into it.

Now we're getting somewhere. Note that the last bit of that sentence describes precisely what you need to do, and it doesn't contain one word of Java.

So I did thisBut my problem here is how do I implement this and this isn't really automatic but I think this is what the question is suggesting I do.

It is, and you're now very close to a solution.

Suggestion: Instead of just creating the new array (which you've got spot on), why not replace your old one with the new, larger one?

And in answer to your last question: Most collections of this kind (a dynamic array) have a method called something like ensureCapacity(). Indeed, ArrayList has a public one, so you can read up on it in the API documentation. IMO, there's no particular need to make it public, but you could certainly write one of your own that you use internally.

Have a look at it and see if you can't work out how you could implement one yourself.

HIH

Winston
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this is how I am using the second array: (edit: Is this the replacing the old array you were referring to Winston?)


So bigStudents is called when students can't hold anymore and this is what i did to ensure bigStudents can never be full


I put students.length+50 just in case multiple students were created at the same time.

Ok so now my question is when I am calling my dropStudent method which is:


I should use students[i].equals(student) and shouldn't use bigStudents here. Is this correct? Please correct me if I am wrong.

Thanks for all the help so far guys.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:So bigStudents is called when students can't hold anymore and this is what i did to ensure bigStudents can never be full

Did you read my last post? Don't use the new array; REPLACE your old one (students) with the new one, and use that. You can actually do it all in a single statement.

You also appear to be trying to do this all in line, and that's unlikely to work. Write a method that increases the size of students when (and ONLY when) it needs to, and call IT.

But that aside, using 'students.length + 50' strikes me as a perfectly sensible way to proceed.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:I should use students[i].equals(student) and shouldn't use bigStudents here. Is this correct?

Seems fine to me; and the fact is that you shouldn't be using bigStudents anywhere except where you actually increase the size of students.

However, I can see a potential problem with your delete method because what it will do is leave your array with "gaps" in it. Now you could possibly rewrite add() to search for gaps and "fill" them, but that's likely to lead to unpredictable ordering.

If element order is important, and you don't want gaps (and trust me, you probably don't), then you really have no choice but to "shift" all the elements to the right of the one you're deleting left by one.

Alternatively, if order isn't important, you can replace the element you want "deleted" with the last non-null one in the array and then null out that one. That will make sure that all your "null" elements are at the right-hand end of the array.

HIH

Winston
 
Paul Ryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help so far Winston. Would this be a better way of increasing the array size essentially giving the user the power to decide how big the array is :



And for the ordering I have been searching around all morning to see how to completely take out an element but I was giving the impression that you can only delete from an arrayList. How would I go about shifting all the elements. My output so far is this:
Johnny Jones, Paul Ryan, Ryan Paul, Paul Jones, Student 1, Student 2, Student 3, Student 4, Student 5,
Number of students before deleting Paul Ryan 9
Paul Ryan is deleted
Number of students after deleting Paul Ryan 8
Output of array now that Paul Ryan is deleted
Johnny Jones, null, Ryan Paul, Paul Jones, Student 1, Student 2, Student 3, Student 4,


I would really like to get rid of that null value.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote: . . . public void increaseArraySize(int amount)
private void, please.
The user should not know about changing the array size. That is an implementation detail which only the list class needs to know about. You might want an increaseCapacity method so you can say increaseCapacity(1000000) before adding 1000000 entries, but that is different.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:I would really like to get rid of that null value.


Instead of setting that entry to null, shuffle everyone else after the student you want to delete along one.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ryan wrote:Thanks for all your help so far Winston. Would this be a better way of increasing the array size essentially giving the user the power to decide how big the array is :

It's one way, but now you're not copying the original. Look at what you did when you created bigStudents, and use the exact same idea.

Also:
1. If you make this automatic (as I believe you were instructed to) there's no real need for the user to decide how big to make the array, is there? So my advice would be to make that method (or one like it) private.

2. That method (once you've got it right) will certainly increase the size of the array, but it doesn't contain any logic to decide when it should do it. The idea is that you should only increase the size when there is a need, so why not let the method do that too?

Did you read the documentation for ArrayList.ensureCapacity()? That might give you some ideas.

I would really like to get rid of that null value.

Well, I gave you two possible ways to do it in my last post. Is there something you don't understand about what I wrote?

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!