aspose file tools*
The moose likes Beginning Java and the fly likes How to get List elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How to get List elements

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
returns me a list containing strings, and may be an int. How can i get those elements?

It cannot be casted to String or Integer.


kunal
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Kunal Lakhani wrote: returns me a list containing strings, and may be an int. How can i get those elements?

It cannot be casted to String or Integer.


Using enhanced for loop
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your reply Gaurangkumar Khalasi.

That returns something like this : [Ljava.lang.Object;@83d8be
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
You do not say how you obtained this string; I recommend giving us code, hopefully running code, so we can see how it is obtained. It appears to be a string returned from an array of Object.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Kunal Lakhani wrote:[Ljava.lang.Object;@83d8be

In other words, an Object[]. You can tell quite easily: the starting [ indicates it's an array. The following is the element type name: an L to indicate it's an object type, followed by the fully qualified class name and a ;. For example, [Ljava.lang.String; indicates a String[].

Instead of an L followed by the class name and a ;, you can also get one of these:
- B for byte
- S for short
- I for int
- J for long (L is taken)
- F for float
- D for double
- C for char
- Z for boolean (B is taken)

So, if you ever see [I, that indicates an int[].

To go one step further, if you encounter multiple occurrences of [ that means that you get an array of an array (of an array etc):
- [I indicates int[]
- [[I indicates int[][]
- [[[I indicates int[][][]


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Rob for your reply
I obtained string through a query from database.

What i did, is converted that Object[] to String array[] and then printed its contents.

Once again Thanks Rob for the valuable information.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Kunal Lakhani wrote:...a list containing strings, and may be an int. How can i get those elements?

Kunal Lakhani wrote:What i did, is converted that Object[] to String array[] and then printed its contents.


Or you can do in the following manner (just one suggestion ):

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Gaurangkumar Khalasi for your nice suggestion.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Kunal Lakhani wrote:Once again Thanks Rob for the valuable information.

You're welcome.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Kunal Lakhani wrote:Thanks Gaurangkumar Khalasi for your nice suggestion.

You are Welcome....
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I have a problem here

This is my database query



This query returns a list(named, (lastPaymentDetails) containing 2 integer and a String.




Above code does not print anything other than "records exists". But it should print the 2 ints and the string

What is the mistake there in the code?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Are you sure the contents are instances of either int[] or String[]? If it's an Object[], instanceof String[] will return false even if all elements are Strings.

In fact, I would change the method. Create a new class (PaymentDetails or something) that has fields that coincide with the two ints and the String. Then let the method create a List of this class (e.g. List<PaymentDetails>). The loop will create one instance of this class for each row.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Rob for your reply



This is what i tried



The sql query is related to TransportPaymentEntity



It gives an exception(ClassCastException), that Object cannot be casted to TransportPaymentEntity
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
No reply yet
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Are you using Hibernate or some other ORM framework? What does the method getLastPaymentData of the DAO return exactly? Apparently it's not a list of TransportPaymentEntity objects.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Kunal Lakhani wrote:No reply yet

Quite possibly because you've made it difficult to read.

Please don't put enormously long lines inside code blocks; it screws up the windowing.
In fact, I suggest you read the UseCodeTags page again. Thoroughly.

I've split them up this time.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your replies Winston Gutkowski, Jesper de Jong.

I have posted the code and its details in another forum, so request you to please continue post in that forum

webpage
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Jesper de Jong, yes i am using hibernate. getLastPaymentData() returns a list. As i have said, in this method, i am passing a SQL query, and it fetches me value of property of TransportPayementEntity. I can get those values by number of if conditions ,(using instance of Integer,or string). But, that seems very odd.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Kunal Lakhani wrote: . . . I have posted the code and its details in another forum, so request you to please continue post in that forum . . .
So why are you still posting here? I think I ought to close this discussion.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get List elements