This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading arrays

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please supply some code.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since you wrote the class...did you implement a toString() method?
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(): -

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic