• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

need some dynamic array help with Java

Status
Not open for further replies.

Ecrofirt

Member
is it possible?

My professor assigned us work for this week, and it looks like it needs a dynamic array. What we've got to do is have the user enter a range of numbers (say 1-100), and the program calls a method which checks to see if the current number is a prime number, and if it is, puts the number in the prime array.

The problem is, you can't just initialize it to someting like int prime[] = new int[10], because you'll run into problems if the number of prime numbers you end up having is higher than the array length.

This brings me to my need for dynamic arrays. I know you can do them in Visual Basic like this:
Dim intPrime() as Integer
and then use Redim Preserve to make it bigger without losing the data in the array. Unfortunately, our book doesn't discuss this.

I've got class in an hour, so if someone could help, I'd be ever so grateful.
 

Ecrofirt

Member
I don't mean to sound gay, but does anyone have any idea? I'm very quickly running out of time.

I emailed my professor about it two days ago, and the idiot never mailed me back. He gave us a very poorly worded assignment this week, and I'm struggling to get this done.
 

Ecrofirt

Member
I'm gonig to type up the sheet he gave us after I go to class.

It's the most poorly written piece of crap in the world.
 

Ecrofirt

Member
well fuck. That's not going to work.

This guy is the best java professor in the world! :rolleyes

There's no dynamic arrays, yet we don't know how many elements of the array will be used, so it's either a waste of memory or we could get an error for going out of the boundry. Why would he assign us such a crap program?
 
Create an array and keep track of the number of values in it. If you run out of space create a new array that's larger, copy the old array into it, and then add the new value. Then destroy the old array.

edit: That is, if you can't use the vector class and feel bad about wasting memory.
 

Hooker

Member
Can't you use "int prime[] = new int[]" -- I haven't done Java in a LONG time but I think it takes this command
 

Hooker

Member
I've just looked into my books, and you can't change the dimensions of an already declared array in Java. You need to make a new one once you need more spaces.
 

GG-Duo

Member
Right, that's what people are saying.

the only problem is the way ArrayList/Vectors grow. They double in size as you exceed the current limit ....

but there's a LinkedList class too.
 
Status
Not open for further replies.
Top Bottom