Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

How to get List elements

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for your reply Gaurangkumar Khalasi.

That returns something like this : [Ljava.lang.Object;@83d8be
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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[][][]
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks Gaurangkumar Khalasi for your nice suggestion.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Kunal Lakhani wrote:Once again Thanks Rob for the valuable information.

You're welcome.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Report post to moderator
Kunal Lakhani wrote:Thanks Gaurangkumar Khalasi for your nice suggestion.

You are Welcome....
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
No reply yet
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic