Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get List elements

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for your reply Gaurangkumar Khalasi.

That returns something like this : [Ljava.lang.Object;@83d8be
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Gaurangkumar Khalasi for your nice suggestion.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
No reply yet
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic