Brian Brown
Brian Brown

Reputation: 4311

Find max element in Java ArrayList

I have such simple code:

import java.util.ArrayList;
import java.util.List;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Integer[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};
        Integer max = 2324;
        List<Integer> indexes = new ArrayList<Integer>();
            for (int e = 0; e < tab.length; e++) {
                if (tab[e] == max) {
                    indexes.add(new Integer(e));
                    System.out.println("Found max");
                }
            }
    }
}

The main problem here is I want to find every index in my tab where the max value is. For now on, it doesnt work - it doesnt display Found max message even once, although it should do it 3 times. Wheres the problem?

Ok, this finally worked, thanks all of you people:

public static void main(String[] args) {
        Integer[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};
        Integer max = 2324;
        List<Integer> indexes = new ArrayList<Integer>();
            for (int e = 0; e < tab.length; e++) {
                if (tab[e].intValue() == max.intValue()) {
                    indexes.add(Integer.valueOf(e));
                    System.out.println("Found max");
                }
            }
    }

Upvotes: 2

Views: 2312

Answers (8)

darijan
darijan

Reputation: 9775

The JVM is caching Integer values.

== only works for numbers between -128 and 127

See explanation here: http://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching

Upvotes: 2

Manuel Reis
Manuel Reis

Reputation: 574

You are using the == operator in something that it is not the primitive int, but an instance of class Integer. Basically, you are comparing the references of both objects, which are different. Try using :

if(tab[e].equals(max))

Upvotes: 3

jlordo
jlordo

Reputation: 37813

Change

Integer[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};

to

int[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};

Integer objects are only precached for the values from -128 to 127.


If you want to leave it Integer you can change

if (tab[e] == max) {

to

if (tab[e].equals(max)) {

because it will then check for object equality, and not reference equality.

Upvotes: 9

rawphl
rawphl

Reputation: 331

You can only compare primitive values with ==. Since Integer is an object, change tab[e] == max to tab[e].equals(max).

Look for equals vs ==

Also read: Java: int vs integer

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533520

The basic problem you have is that you are using Integer, not int One difference being that as Integer is an object == compares the references to two different objects. (Not he contents of those objects)

I suggest you use primitives like int instead of objects where you can.

Upvotes: 2

vishal_aim
vishal_aim

Reputation: 7854

try this:

if (tab[e].intValue() == max.intValue()) {

or

if (tab[e].intValue() == max) {

If you are using Integer object rather than primitive int, then with comparison operator like == , atleast one operand should be primitive one (other will be converted implicitly).

Or you should use equals method for equality

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49372

Use one of the three : 1. tab[e].intValue() == max or 2. int max = 2324; or 3. Use equals() method of Integer class.

Upvotes: 1

Michal Borek
Michal Borek

Reputation: 4624

That's because you are comparing with == and not equals.

Upvotes: 3

Related Questions