aspose file tools*
The moose likes Beginning Java and the fly likes How can you make an array grow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can you make an array grow" Watch "How can you make an array grow" New topic
Author

How can you make an array grow

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
Are you positive becuase my teacher said something about a method he said it started like private void grow()
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
An array is an object but has no methods associated with it.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Maybe he meant that the grow() method was one you needed to implement using other methods in the API.
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
What does API mean....i'm sorry but I am new at this
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
API is the Application Programming Interface.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Feb 03, 2006
Posts: 106
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

Joined: Feb 07, 2005
Posts: 2367
What class definition is the method in?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
again huh?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I mean what class definition contains that method?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 03, 2006
Posts: 106
i'm sorry

grow is a method I need to write
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The code that Garrett showed should give you a start.
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
okay thanks I will play around with it....I appreciate the help
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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).


Tony Morris
Java Q&A (FAQ, Trivia)
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 03, 2006
Posts: 490
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.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
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


National Research Council<br />Internet Logic Department
Martin Mathis
Ranch Hand

Joined: Dec 20, 2004
Posts: 45
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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can you make an array grow