You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Object数组数值比较报错:如何实现大于/小于判断?

Fixing the "Incomparable types" Error in Your Age Comparison Code

Let's break down the issues in your code and fix them step by step:

First, two critical mistakes are causing your error:

  • Wrong array index: You're trying to compare Person[i][0] (which stores the person's name, a String) to the number 18. Strings and integers can't be compared directly—you meant to use Person[i][1], which holds the age as an Integer object.
  • Object type limitation: Your array is of type Object[][], so elements are treated as generic Objects. These don't support numerical comparison operators like < or >. You need to cast the Object back to an Integer (or int) first to perform the check.

Solution 1: Cast the Object to Integer for Direct Comparison

You can safely cast the age element to an int (or Integer) to run the numerical comparison. Here's the corrected version of your original code:

Object[][] Person = new Object[2][2];
Person[0][0] = "John";
Person[0][1] = new Integer(18);

Person[1][0] = "Mike";
Person[1][1] = new Integer(42);


for(int i = 0; i < Person.length; i++)
{
    System.out.print(Person[i][0]);
    System.out.print("\t" + Person[i][1] + "\t");

    // Cast the Object to int first, then compare
    int age = (int) Person[i][1];
    if(age < 18)
    {
        System.out.print("Under 18");
    } else {
        System.out.print("18 or over");
    }    

    System.out.println();
}  

If you prefer working with the Integer object directly, you can use its compareTo() method:

Integer age = (Integer) Person[i][1];
if(age.compareTo(18) < 0) {
    System.out.print("Under 18");
} else {
    System.out.print("18 or over");
}

Solution 2: Use a Custom Person Class (Cleaner, Type-Safe Approach)

For more maintainable and error-free code, it's better to create a dedicated Person class instead of relying on an Object[][] array. This eliminates casting entirely and makes your code more readable:

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }
    public int getAge() { return age; }
}

public class Main {
    public static void main(String[] args) {
        Person[] people = new Person[2];
        people[0] = new Person("John", 18);
        people[1] = new Person("Mike", 42);

        for(Person person : people) {
            System.out.print(person.getName());
            System.out.print("\t" + person.getAge() + "\t");

            if(person.getAge() < 18) {
                System.out.print("Under 18");
            } else {
                System.out.print("18 or over");
            }

            System.out.println();
        }
    }
}

This approach is far more scalable if you ever need to add more attributes to your Person objects later.

内容的提问来源于stack exchange,提问作者Bradie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:13:21