Open Side Menu Go to the Top
Register
Problem accessing Arraylist Java Problem accessing Arraylist Java

10-17-2014 , 11:48 PM
Hi

Im a programming noob and having trouble retrieving data from an Arraylist. I am wanting to compare the first and second items in the ArrayList and have 'bolded' where the problem is and wont coompile. This is my code, thanks for any help:

List hand = new ArrayList();
hand.add(fCard1);
hand.add(fCard2);
hand.add(fCard3);
hand.add(tCard);
hand.add(rCard);
hand.add(p.getHole1());
hand.add(p.getHole2());
Collections.sort(hand);

if (hand.get(0) == (hand.get(0))-1 % 52)
{
nextCardIsStraight1 = true;
}
Problem accessing Arraylist Java Quote
10-17-2014 , 11:54 PM
Try .equals method but also why not use an IDE and try debugging for your very first time.
Problem accessing Arraylist Java Quote
10-18-2014 , 01:04 AM
Probably a problem with types mismatched in the bolded.
Problem accessing Arraylist Java Quote
10-18-2014 , 01:18 AM
Yah why are you comparing the same thing but anyway learn to debug, its simple step over, step out, step in and breaker points. Will stop you from making these thread if you learn it. Also inspect and watch are good to pick up for debugging tool belt.
Problem accessing Arraylist Java Quote
10-18-2014 , 02:08 AM
Quote:
Originally Posted by maxtower
Probably a problem with types mismatched in the bolded.
Yes, i get the following error: Operator cannot be applied to java.lang.Object,int

I have added items of type int to this arrayList so why would the types be mismatched?

Thanks
Problem accessing Arraylist Java Quote
10-18-2014 , 06:40 AM
Quote:
Originally Posted by iosys
Yah why are you comparing the same thing but anyway learn to debug, its simple step over, step out, step in and breaker points. Will stop you from making these thread if you learn it. Also inspect and watch are good to pick up for debugging tool belt.
Ye I cocked up, obv meant to compare with hand.get(1)-1 % 52. Casting the returned value to (Integer) seems too have worked, but still don't know why an Object seems to be returned and not an int.
Problem accessing Arraylist Java Quote
10-18-2014 , 08:21 AM
I suspect it has to do with Java doing auto boxing.

In Java the collection objects like an ArrayList need to take objects. So when you put in int in the list it actually gets wrapped as an Integer object. When you call get its returning an Integer instance (not the primitive int).

The other part is that your list isn't templated. So when you call get Java can only tell you that you have an Object. It doesn't know that it's an Integer object and so it doesn't know how to perform subtraction with an Object and an int.

When you tell Java that it's an Intrger object it knows it can do the reverse of auto boxing.

Without seeing the exact error or trying it myself this is just my best guess.
Problem accessing Arraylist Java Quote
10-18-2014 , 08:26 AM
In one of my first old man moments I'll point out that auto boxing and templating were added in Java 1.5(or so) and we use to have to manually wrap and cast all the ****ing time with lists.
Problem accessing Arraylist Java Quote
10-18-2014 , 10:11 AM
List<Integer> = new ArrayList<>();

Problem solved.
Problem accessing Arraylist Java Quote
10-18-2014 , 04:01 PM
Quote:
Originally Posted by jjshabado
I suspect it has to do with Java doing auto boxing.

In Java the collection objects like an ArrayList need to take objects. So when you put in int in the list it actually gets wrapped as an Integer object. When you call get its returning an Integer instance (not the primitive int).

The other part is that your list isn't templated. So when you call get Java can only tell you that you have an Object. It doesn't know that it's an Integer object and so it doesn't know how to perform subtraction with an Object and an int.

When you tell Java that it's an Intrger object it knows it can do the reverse of auto boxing.

Without seeing the exact error or trying it myself this is just my best guess.
this sounds right.

Quote:
In one of my first old man moments I'll point out that auto boxing and templating were added in Java 1.5(or so) and we use to have to manually wrap and cast all the ****ing time with lists.
lol wow.
Problem accessing Arraylist Java Quote
10-18-2014 , 09:52 PM
Thankyou so much jjshabado for the explanation.

Also I will try that EvalEvan, thanks
Problem accessing Arraylist Java Quote

      
m