Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can you make an array grow

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to have grow method in my program. I know I would not need this if I used an arrayList but I cannot use that. I have been instructed to use arrays. So can anyone tell me how to make my array bigger.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you cannot change the size of an array once it's created. You can however create another array that is larger than your original and use System.arraycopy to copy the contents from the first into the second. Then assign the reference to your first array to point to the second.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The size of an array is fixed. Your only option is to copy the values into a larger array using System.arraycopy and then assign the original reference to the larger array. Like this

[ February 04, 2006: Message edited by: Garrett Rowe ]
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you positive becuase my teacher said something about a method he said it started like private void grow()
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is an object but has no methods associated with it.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe he meant that the grow() method was one you needed to implement using other methods in the API.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does API mean....i'm sorry but I am new at this
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
API is the Application Programming Interface.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
Maybe he meant that the grow() method was one you needed to implement using other methods in the API.


Sorry, didn't mean to confuse you. By API I meant the classes and methods in the standard java library.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i know it starts out like


What would I put in the middle what time of method would I call
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class definition is the method in?
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again huh?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean what class definition contains that method?
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you asking what toher methods are avaliable...
I know that the name of my class is EmployeeList....I have another one named Employee

I did not import anything to build these
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'm confused about the method grow. Is that a method you are supposed to write or are you looking for a class that has that method?
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm sorry

grow is a method I need to write
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that Garrett showed should give you a start.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks I will play around with it....I appreciate the help
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your only option is to copy the values into a larger array using System.arraycopy and then assign the original reference to the larger array.

Not quite true - if you digress to requirements, you'll find that arrays contain a number of abstraction leaks (exceeding requirements), and the need to copy is one consequence of this. If you use a more appropriate structure that does not have such a leak, then this restriction disappears. I've already ranted about it elsewhere, so I won't do it again.

An array is an object but has no methods associated with it.

An array object has all the methods of java.lang.Object and the clone() method has been overridden with a lesser access scope (public).
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so Tony what would you do.....

I have had no luck at all i'm stressed i'm going to cry
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

An array object has all the methods of java.lang.Object and the clone() method has been overridden with a lesser access scope (public).


Sorry for the mistake. I was intending to say that there are no methods like the one she mentioned, (private void grow)
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have to use an array, just mess around with an earlier suggestion.

This is a great assignment to learn from. It touches on what an ArrayList does, and that is far more valuable then just learning how to use ArrayList.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you know how much the array has to grow by? Should it be specified in a parameter to the grow method...eg: public void grow(int growByThisMany)
or maybe...public void grow(int newSize), where newSize must be >=the old array size.

Also do you just have to initialize the rest of the new "grown" array elements to anything?Like did the question specify? I am just curous about these things.

Also if your not allowed to use the arrayCopy() method, you can just loop though each element in the old array, and add each element of the old one into a new one with the new specified size.

Let me know if your confused
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Victoria Preston:
so Tony what would you do.....

I have had no luck at all i'm stressed i'm going to cry


All you need to do is...

1. Create a new array (larger)
2. Copy the contents of the old array into the new one (System.arraycopy(..), or a loop)
3. Make the reference to the original array point to the new one

All you have to do is convert those 3 lines to code and you're finished.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Victoria Preston:
so Tony what would you do.....

I have had no luck at all i'm stressed i'm going to cry


Easy, I'd write a structure that has much less abstraction leak (though still not optimal as per Java), and use that. In fact, I'd call it a sequence in a project called ContractualJ, then I'd publish it for anyone else who chooses to learn why arrays are horribly broken. Data copying (as you no doubt have been advised to use via System.arraycopy) is a symptom of the leak - not a solution. That's what I'd do

http://contractualj.com/api/net/tmorris/adt/sequence/Sequence.html
[ February 07, 2006: Message edited by: Tony Morris ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, please. Victoria is in a beginning Java programming class, and they want her to learn the how-to-implement-a-vector thing; feeding her this "abstraction leak" stuff sure isn't going to help her succeed in her immediate goals. You have to walk before you can run.

And I would imagine that a Sequence is implemented using a Java array, anyway, right?

Victoria, Martins's three steps


1. Create a new array (larger)
2. Copy the contents of the old array into the new one (System.arraycopy(..), or a loop)
3. Make the reference to the original array point to the new one


Are exactly what you need to do. It's the right way, it's the standard way, and it's what you need to do.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
[QB]Tony, please. Victoria is in a beginning Java programming class, and they want her to learn the how-to-implement-a-vector thing; feeding her this "abstraction leak" stuff sure isn't going to help her succeed in her immediate goals. You have to walk before you can run.

Point taken. However, I have observed that the vulnerable mind is much more prone to accepting reality than the trained/misled mind. That is, I can explain to my university students a concept that claimed "experienced" people still fail "to get". My conclusions are based on the kinds of questions that are asked. In any case, I concede.

And I would imagine that a Sequence is implemented using a Java array, anyway, right?

Not at all. A Sequence has nothing to do with arrays - why would I put a coating of strawberry cream on a big poo? A Sequence can indeed "grow" and "shrink" despite being an immutable (for some definition of immutable) structure without the need for data copying as a result of a more appropraiate abstraction (i.e. less leakage).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic