This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array questions" Watch "Array questions" New topic
Author

Array questions

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
1. I have a question on a code that my teacher wrote:


So my main question is that on line 09, it is creating a new values object which is contained in the Array object which later instantiated at the static main method. So I don't get the purpose of doing it, like why object containing another object? Why can't the Array class just have its own object as an array: Array[] A = new Array[50]? On line 20, what will it print? the values array?

2. Imagine an array of 5 elements: 3 1 5 8 9. If I want to shift the second element (index=2) down one, does it mean I want it to look like: 3 1 _ 5 8 9, which means the size of the array will increase? But what happens to the original position that 5 was at? Is it just left blank or..? But array's size is fixed, so how do you write this shiftdown method?
Thanks!
Matt Moran
Greenhorn

Joined: Mar 12, 2005
Posts: 13

1. OK - complex set of questions here, but essentially what your code does is describe a class called Array which has a field that holds an Object array, a field to hold the count of the contents of this Object array, and a flag to indicate whether it's sorted or not. You use an Object array because every object in Java is an instance of the class Object or its subclasses, so you can refer to anything as an Object & therefore an Object[] array can hold all sorts of things. An Array of Array objects would only be able to hold Arrays, which gets mind-bogglingly recursive but not very useful unless all you'll ever deal with are Arrays.

As the code stands, since the Array class doesn't have a handy toString() method that iterates through it printing out the contents of the values field, you'd get a weird looking reference to your Array object rather than anything helpful.

2. Array indexes start at 0 so the 2nd element is index 1. If I remember rightly you can't do the operation you're after using one single array - you'd be better off creating a new array of size max+1 as a temporary variable, copying your values over as needed & returning it as if it were the parameter array.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Matt Moran wrote:1. OK - complex set of questions here, but essentially what your code does is describe a class called Array which has a field that holds an Object array, a field to hold the count of the contents of this Object array, and a flag to indicate whether it's sorted or not. You use an Object array because every object in Java is an instance of the class Object or its subclasses, so you can refer to anything as an Object & therefore an Object[] array can hold all sorts of things. An Array of Array objects would only be able to hold Arrays, which gets mind-bogglingly recursive but not very useful unless all you'll ever deal with are Arrays.

As the code stands, since the Array class doesn't have a handy toString() method that iterates through it printing out the contents of the values field, you'd get a weird looking reference to your Array object rather than anything helpful.


Hi Matt, so you are saying that because having an array of Array objects will not be so helpful/reusable, it's better to create an array of Objects instead within the Array class,so that this array of Objects can also use any methods that the Array class has? Is that the whole point?
Matt Moran
Greenhorn

Joined: Mar 12, 2005
Posts: 13

Yeah, pretty much - I'm assuming here but I think the main purpose of the Array class you have there is to provide a container for other classes (possibly including more arrays if you want a 2D array). The Array class can act upon the Objects it contains assuming a suitable method is created (e.g. a sort method) but you wouldn't expect the Objects to act upon the Array as a whole since you'd expect them to be usable singly. Since Array is a subclass of Object, you *can* hold more Arrays but you're not forced to. They could be Dogs or JButtons or whatever you like. When you want them to act as Dogs etc you need to cast them back to their original classes, so e.g. if we had a Motorbike in values[2] & you wanted to rev its engine, it wouldn't be myArray.values[2].rev(), but rather myBike = (Motorbike)myArray.values[2]; myBike.rev(); - you have to cast it to access methods that aren't available on Object.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Matt Moran wrote:Yeah, pretty much - I'm assuming here but I think the main purpose of the Array class you have there is to provide a container for other classes (possibly including more arrays if you want a 2D array). The Array class can act upon the Objects it contains assuming a suitable method is created (e.g. a sort method) but you wouldn't expect the Objects to act upon the Array as a whole since you'd expect them to be usable singly. Since Array is a subclass of Object, you *can* hold more Arrays but you're not forced to. They could be Dogs or JButtons or whatever you like. When you want them to act as Dogs etc you need to cast them back to their original classes, so e.g. if we had a Motorbike in values[2] & you wanted to rev its engine, it wouldn't be myArray.values[2].rev(), but rather myBike = (Motorbike)myArray.values[2]; myBike.rev(); - you have to cast it to access methods that aren't available on Object.


And on line 19, what does '50' say about this Array object anyway since it is not a real array? I mean what's the significance of calling it? So that we can use the values array that it contains?
Matt Moran
Greenhorn

Joined: Mar 12, 2005
Posts: 13

When you use Array [50] you're just creating an array of Array objects, each containing an array of null objects (the values field of each).
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Matt Moran wrote:When you use Array [50] you're just creating an array of Array objects, each containing an array of null objects (the values field of each).


Note you use the bracket [50], but in the code, it's just ( ), like any other objects we have seen before. So I don't think it means array.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Cheryl Scodario wrote:...in the code, it's just ( ), like any other objects we have seen before. So I don't think it means array.

Correct! Array(int max) is the constructor for your Array class, so line 19 is creating a new instance of Array. The constructor uses its parameter to create an array of Objects in line 9.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Cheryl Scodario wrote:...on line 19, what does '50' say about this Array object anyway since it is not a real array? I mean what's the significance of calling it? So that we can use the values array that it contains?

Yes. Line 19 creates a new Array Object (an instance of your class). The parameter of 50 is used in the constructor to create a new Object array of length 50.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
marc weber wrote:
Cheryl Scodario wrote:...on line 19, what does '50' say about this Array object anyway since it is not a real array? I mean what's the significance of calling it? So that we can use the values array that it contains?

Yes. Line 19 creates a new Array Object (an instance of your class). The parameter of 50 is used in the constructor to create a new Object array of length 50.


Hi marc, again! so basically the significance of this parameter (50) is for the construction of the Object array with a size 50, not really useful for the Array instance itself?
Matt Moran
Greenhorn

Joined: Mar 12, 2005
Posts: 13

Cheryl Scodario wrote:
Matt Moran wrote:When you use Array [50] you're just creating an array of Array objects, each containing an array of null objects (the values field of each).


Note you use the bracket [50], but in the code, it's just ( ), like any other objects we have seen before. So I don't think it means array.


Ah, you're right - was posting from my mobile phone: the mobile version of this site doesn't include the thread on the page when you reply - my bad!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Matt Moran wrote:When you use Array [50] you're just creating an array of Array objects, each containing an array of null objects (the values field of each).
Not quite. You are creating a reference to a 50-member array, all 50 elements being empty (or null). You have to put an Array object into each of those 50 references before you try to use it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Cheryl Scodario wrote:...so basically the significance of this parameter (50) is for the construction of the Object array with a size 50, not really useful for the Array instance itself?

Well, let's take a step back (to what might have been your original question) and look at the Array class definition...

This says that each instance (object) of Array will contain 3 private members: An Object array called "values," an int called "count," and a boolean called "sorted."

A constructor's job is basically to get the object ready for use. In this case, whenever a new instance of Array is created, its constructor will create a new Object array with a length of "max" (the value passed to the constructor). A reference to that array is assigned to the variable "values." The constructor will also set "count" to 0 and "sorted" to true.

So the parameter (in this example, 50) is significant to the Array instance, because it sets the size of that instance's Object array, which is presumably something that the Array object needs to do what it does.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
marc weber wrote:
Cheryl Scodario wrote:...so basically the significance of this parameter (50) is for the construction of the Object array with a size 50, not really useful for the Array instance itself?

Well, let's take a step back (to what might have been your original question) and look at the Array class definition...

This says that each instance (object) of Array will contain 3 private members: An Object array called "values," an int called "count," and a boolean called "sorted."

A constructor's job is basically to get the object ready for use. In this case, whenever a new instance of Array is created, its constructor will create a new Object array with a length of "max" (the value passed to the constructor). A reference to that array is assigned to the variable "values." The constructor will also set "count" to 0 and "sorted" to true.

So the parameter (in this example, 50) is significant to the Array instance, because it sets the size of that instance's Object array, which is presumably something that the Array object needs to do what it does.


Thanks, Marc. This is very clear.
Jennifer Schwartz
Ranch Hand

Joined: Jan 12, 2011
Posts: 46

Can someone tell me if "count" and "sorted" are instances of value?? If so, is this why there is no constructor for these two? What am I missing here?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
No, they aren't. They are an int and a boolean. Whether sorted changes when the array is filled, I am not sure. But it is correct that sorted be true initially; an empty array has no elements out of place!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Jennifer Schwartz wrote:Can someone tell me if "count" and "sorted" are instances of value?? If so, is this why there is no constructor for these two? What am I missing here?

As Campbell pointed out, "count" is type int, and "sorted" is type boolean. These do not have constructors because they are primitive (non-Object) types.
Jennifer Schwartz
Ranch Hand

Joined: Jan 12, 2011
Posts: 46

Thanks to both of you. Another rule I can't get stuck in my head yet! lol
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Which rule can't you get your head round?
I suspect you are trying to learn too much theory and not writing enough code. Write lots of simple code and see what happens when you execute it
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array questions
 
Similar Threads
simple sorting algorithm
Sun Cirtification
Hi Thread doubt
Expalin the answer -- Arrays
array question