【问题标题】:create array with two different data types. convert into HashMap, TreeMap, LinkedHashMap创建具有两种不同数据类型的数组。转换成HashMap、TreeMap、LinkedHashMap
【发布时间】:2015-11-30 15:17:05
【问题描述】:

如何创建数组:

{int, String}, {String, String}, {int, long}, {String,boolean}, {String, double}, {int, class Car}, {String, class Car}

这是汽车类:

public class Car {
    int id;
    String car_name;
    String number;

    public Car(int id, String car_name, String number) {
        this.id = id;
        this.car_name = car_name;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", car_name='" + car_name + '\'' +
                ", number='" + number + '\'' +
                '}';
    }
}

而且。如何通过自定义方法将其转换为 HashMap、TreeMap、LinkedHashMap ?

【问题讨论】:

  • 完全看不懂。你能改写一下吗?
  • 创建具有两种数据类型的数组。示例:{int,字符串}。有int(原始数据类型)和String。如何创建这个数组?

标签: java arrays collections converter typeconverter


【解决方案1】:

您不能为不同的数据类型创建数组。根据文件,

数组是一个容器对象,它包含固定数量的单一类型值。数组的长度是在创建数组时确定的。创建后,它的长度是固定的。

您可以根据需要使用 Hashtable 或 HashMap。

//Code to put entries in Hashtable
Hashtable<String, Object> car = new Hashtable<>();
car.put("id", id);
car.put("car_name", car_name);
car.put("number", number);

//Code to retrieve data from Hashtable
int id = (int)car.get("id");
String car_name = (String)car.get("car_name");
String number = (String)car.get("number");

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 2013-08-12
    • 2019-07-24
    • 2011-02-22
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多