2

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");
                }
            }
    }
Brian Brown
  • 3,873
  • 16
  • 48
  • 79

8 Answers8

9

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.

jlordo
  • 37,490
  • 6
  • 58
  • 83
3

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

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
3

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))
Manuel Reis
  • 574
  • 8
  • 29
2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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

Community
  • 1
  • 1
rawphl
  • 331
  • 2
  • 8
2

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

darijan
  • 9,725
  • 25
  • 38
1

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

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

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

vishal_aim
  • 7,636
  • 1
  • 20
  • 23