有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查ArrayList中与String对象关联的值?

我有一个包含多个不同类型对象的ArrayList。 在一个索引处,我有一个具有字符串值的元素。我知道索引位置,我知道字符串与整数或双精度值关联

如何检查与该对象关联的值是双精度还是整数

编辑:我并不是在尝试将5.5与5进行比较,当我尝试检查对象的数据类型时,我收到一个编译器错误,说明我无法解析对象

我的arraylist由两个字符串对象组成。我所做的是解析字符串对象,而不是与该对象关联的字符串值

EDIT2:更正!!!这些对象实际上不是字符串对象。相反,它们只是没有特定类关联的对象。如果不在arraylist中声明对象类型,arraylist将自动将每个元素分配给对象值(而不是一个对象是字符串、一个对象是双精度对象、一个对象是int等)

~~~

我的代码示例:

import java.util.ArrayList;
public class idk {

  public static void main(String[] args) {
    ArrayList myList1 = new ArrayList();
    ArrayList myList2 = new ArrayList();

    myList1.add("5.5");
    myList2.add("5");

    //How does one check if the value associated to the String object 
    //in the arrayList (like myList.get(1)) is a double or an integer?
  }
}

我只尝试实现isDouble方法,但它检查对象是否是double而不是string对象的值

import java.util.ArrayList;

public class idk {

    //I've attempted to try using this method to check the value but I get
    //a compiler error saying I can't use the isDouble method which checks
    //for a String value, not the value of the actual String object in the arraylist
    boolean isDouble(String str) {
      try {
        Double.parseDouble(str);
          return true;
      }
      catch (NumberFormatException e) {
        return false;
      }
    }


    public static void main(String[] args) {
      ArrayList myList1 = new ArrayList();
      ArrayList myList2 = new ArrayList();

      myList1.add("5.5");
      myList2.add("5");

      if (isDouble(myList1.get(0))==true) {
        System.out.println("The object in pos 0 is a double");
      }else {
        System.out.println("The object in pos 0 is not a double");
      }

      if (isDouble(myList1.get(0))==true) {
        System.out.println("The object in pos 0 is a double");
      }else {
        System.out.println("The object in pos 0 is not a double");
      }

    }

共 (3) 个答案

  1. # 1 楼答案

    我要做的是尝试将字符串同时解析为整数和Double,然后检查结果值是否相等

    作为双精度的“5”仅为5.0,作为整数的“5”为5。它们相等,因此字符串是整数

    “5.2”作为双精度是5.2,作为整数是5。它们不相等,所以您的字符串不是一个整数-通过构造问题,因此必须是一个双精度字符串

    编辑:我刚刚试过,如果将“5.2”传递给Integer.parseInt(),它将抛出一个异常NumberFormatException。因此,如果字符串抛出异常,则它有一个小数点并表示双精度。否则它是一个整数

  2. # 2 楼答案

    一定要考虑整数和双精度之间的区别。所有的双精度是整数还是所有的整数都是双精度?(一个是正确的)

    如果你能回答这个问题,那么你就会知道你的isDouble方法应该先测试哪种方法,然后再测试哪种方法

    而是先测试它是否是整数,因为并非所有的双精度都是整数

    boolean isInteger(String str) { try{ Integer.valueOf(str); //You should be able to use parseInteger also return true; } catch (NumberFormatException e){ return false; } }

    然后仍然需要确保字符串是double,而不是其他内容。你的方法在这里应该足够了

  3. # 3 楼答案

    I've attempted to try using this method to check the value but I get
    a compiler error saying I can't use the isDouble method which checks
    for a String value, not the value of the actual String object in the arraylist

    只需将从myList1.get(0)返回的对象转换为字符串,然后调用isDouble方法

    if (isDouble(myList1.get(0).toString()) == true) {
    

    或者将isDouble方法参数从String更改为Object,这样就可以了

    boolean isDouble(Object obj) {
       try {
           Double.parseDouble(obj); 
           /* 
             obj must be converted to string first and then you can call 
             parseDouble method on it , so you will get an error like this
             error: incompatible types: Object cannot be converted to String
           */
           return true;
       } catch (NumberFormatException e) {
           return false;
       }
    }
    

    然后尝试以下方法之一来修复我所说的错误

    • 一,。使用铸造优点,调用parseDouble,如下代码所示

      Double.parseDouble((String) obj );
      
    • 二,。使用obj.toString()

      Double.parseDouble(obj.toString());
      

    注意: 对于双精度整数,此方法始终返回true 因为每个整数都是双精度的,所以此方法没有任何用处
    改用isInteger方法,如sbgreene1307{a2}

    你需要改变的是:

    • 将名称从isDouble更改为isInteger
    • Double.parseDouble(str);更改为Integer.parseInt(str);