Forums Register Login

How to get List elements

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Thanks for your reply Gaurangkumar Khalasi.

That returns something like this : [Ljava.lang.Object;@83d8be
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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[][][]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 ):

+Pie Number of slices to send: Send
Thanks Gaurangkumar Khalasi for your nice suggestion.
+Pie Number of slices to send: Send
 

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


You're welcome.
+Pie Number of slices to send: Send
 

Kunal Lakhani wrote:Thanks Gaurangkumar Khalasi for your nice suggestion.


You are Welcome....
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
No reply yet
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


Reply locked
This thread has been viewed 1605 times.
Similar Threads
List and set
Regarding Ordering the List
Split String to ArrayList
Vector array
How to sort a list of lists
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:20:56.