wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Reading arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading arrays" Watch "Reading arrays" New topic
Author

Reading arrays

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Why would you get Vehicle@765291 when reading a position in an array.

I have coming into a method: Vehicle types

In a for loop I have

System.out.println ("\nThe Vehicle is of type: "+types);

My printout to screen is Vehicle@765291

What I would like to see is car or lorry or bike?

When I do +types[i] my output to screen is null?

(I just want to test what I have coming into my method calcVehicle:

public void calcVehicle (Vehicle types)
jeff willis
Greenhorn

Joined: Jul 29, 2004
Posts: 25
Please supply some code.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
public void calcVehicle (Vehicle types)
//The input into this method is coming from another program
//it has been tested ok

Vehicle [ ] types= new Vehicle [numberOfVehicles];

for (int i=0; i<(numberOfVehicles);
{
System.out.println ("The Vehicle type is: "+types);
}//end for loop
}//end method calcVehicle

I have also tried

for (int i=0; i<(numberOfVehicles);
{
Vehicle type = types[i];
//and then printing out types [i] but I got null?

I don't understand how I read an array that is coming into a method. I could have any number for the array size so need the for loop. But what does Vehicle @765291 mean? Is is the place stored in memory, if so something must be coming in and if so I do I get the String value? Do I cast it? And if so why is this necessary?
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Still not enough information.

For this line to be meaningful:


Your Vehicle class must have implemented (or overridden) the toString() method.

Did you write this class?
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I did write this class. I'm trying to get to grips with things in arrays so I'm questioning alot?
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Well, since you wrote the class...did you implement a toString() method?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Maureen, post your actual code. The code you posted earlier will not compile since the variable "types" is declared as a parameter and as a local variable.

Here's an example of how you would print the contents of a String array:



Junilu - [How to Ask Questions] [How to Answer Questions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The version of System.out.println() that takes an Object as a parameter calls toString() on that Object and prints out the resultant String. The default Object.toString() implementation returns a String object with the name of the concrete class for the object and the address of the object (at least, I think that's what the number represents). If you want to print out more meaningful information, you need to override the toString() object in the classes you write. In this case, simply add a toString() method to your Vehicle class that creates a String with the information you wish to display.

HTH

Layne


Java API Documentation
The Java Tutorial
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Junilu Lacar,

(Many thanks for your response)

In the example:



I would like to look at an example that uses objects i.e.

public void printArray (Vehicle vehicleObject)
{
for (int i = 0; i<vehicleObject.length; i++)
{
System.out.println(vehicleObject[i]);
}
}//end printArray

When I use the VehicleObject vehicleObject and not String [ ] arr I get an error message when compiling that says:

array required, but Vehicle found.

I'm a bit confused to why. Are you able to explain?

Also are you able to suggest where I would fine some example code that uses similar objects to vehicleObject?

Thanking you in advance.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This construct:

will only ever be used in conjunction with an array. Its a special "method" for getting the contents the array contains at the place the index points to. Because the object Vehicle is not (and never can be) an array, this is why you get the "array required, but Vehicle found" message.

Take a look at some array-like Collection classes (java.util.ArrayList, java.util.LinkedList, java.util.Vector). All of these have methods to access the data they contain in a simmilar way - e.g. ArrayList uses get(int index). If you want your printArray method to print the contents of an array of Vehical objects, it will have to take an array in as its parameter like this:

If your Vehicle class contains an array of data you want to print, you will have to supply a simmilar accessor method as you find in ArrayList, LinkedList or Vector. You will also have to give it a publicly accessable property of length. Then you could do something like you want:

[ November 11, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Layne Lund:
The default Object.toString() implementation returns a String object with the name of the concrete class for the object and the address of the object (at least, I think that's what the number represents).


Actually, the number is the hash code of the object. The hash code of any object that doesn't override it, is related to the identity of the object, which is related to the address. As the hash code is always 32 bits (an int), but Java is not constrained to run on 32-bit machines, it is wrong to say that the hash code is the address.

Here's Object.toString(): -



Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading arrays