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");
                }
            }
    }
5
  • One hint. Use Integer.valueOf(...) instead of new Integer() since it is more memory efficient (uses cached values for small numbers). Commented May 13, 2013 at 12:13
  • I NEED to use Integers, cant just use int. Commented May 13, 2013 at 12:15
  • stackoverflow.com/questions/8304767/… Commented May 13, 2013 at 12:16
  • 1
    Integer.valueOf(e) instead of new Integer(e). Semantically the same, but JVM will return pre-cached objects of numbers between -128 and 127 Commented May 13, 2013 at 12:17
  • @Raghunandan: OPs title is misleading. Your linked question has nothing in common to this one. Commented May 13, 2013 at 12:17

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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

Comments

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.

Comments

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

Comments

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

Comments

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

2 Comments

Hmm, strange. Changed it as you suggested but nothing changed.
@BrianBrown it changed on my system, can you provide the code again what you tried?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.