• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I increase the size of an array?  RSS feed

 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I know that technically once an array is created the size is fixed however I am reading a book and it is giving a bit of confusing information.

Here is some of the code:




Ok so what I get is that a new array is being created that is double the length of the first array, and that all 6 values from the first array are going to be in it.

Does the values being transferred to new array always start at index 0 unless told otherwise? For example in this case, index 6-11 would be assigned 0 by default while index 0-5 are given the same values that the first array has?

Also is it necessary to import java.util.Arrays?



Mainly I do not understand this part:

values = newValues;

What exactly is this doing? How can values be assigned 12 values if it is a fixed array of size 6?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you copied that code correctly? If so, there's a typo on line 3. It should be newValues, not new Values.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jackson Blackwale wrote:Ok so what I get is that a new array is being created that is double the length of the first array, and that all 6 values from the first array are going to be in it.


Yup.

Does the values being transferred to new array always start at index 0 unless told otherwise? For example in this case, index 6-11 would be assigned 0 by default while index 0-5 are given the same values that the first array has?


The docs for that method spell it out pretty explicitly and clearly.

Also is it necessary to import java.util.Arrays?


No. You could also use the fully qualified class name: but that leads to cluttered code.


Mainly I do not understand this part:

values = newValues;

What exactly is this doing? How can values be assigned 12 values if it is a fixed array of size 6?


It doesn't. The = operator never copies objects in Java. Just references (or primitives). All it's doing here is saying "What array object is the newValues reference variable currently pointing to? Let's point the values reference variable to that same array object."
 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok back to this part:
values = newValues;

What is the difference between say

values = newValues

and

int[] values = newValues (if I was trying to make a new array)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This doesn't create a variable called 'values', it assume there already is one declared somewhere that is accessible in the current scope. it will assign it the same value as what newValues holds (which is basically a memory address). The old value/address in values is clobbered.


This will create a brand new variable called values, and assign it the value in newValues. If there is already a variable called "values", you could potentially hide it, or you could get a compiler error saying "that variable has already been declared".


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And note that when Fred talks about creating a variable, it's just that--a variable, not an object. A declaration like int[] values or MyClass mc never creates an object. It just declares a named location to store a reference to an object. (Or in the case of primitives, a named location to store the value of a primitive.)

So we have

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!