java - 在Java中创建多种数据类型的数组列表

标签 java arraylist

我正在使用的一些文件:http://pastebin.com/WriQcuPs

目前我必须制作人口、纬度和经度字符串,否则我将无法获得所需的输出。我希望它们按照 int、double 和 double 的顺序排列。

public class City {
    String countrycode;
    String city;
    String region;
    String population;
    String latitude;
    String longitude;

    public City (String countrycode, String city, String region, String population, String latitude, String longitude) {
        this.countrycode = countrycode;
        this.city = city; 
        this.region = region;
        this.population = population;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String toString() {
        return this.city + "," + this.population 
                + "," + this.latitude + ","
                + this.longitude; 
    }
}
<小时/>

我怀疑这与我创建数组列表的方式有关。有没有办法使列表中的某些元素具有不同的类型?我尝试将其更改为 ArrayList<City>并更改 City 中的数据类型类,但它仍然给了我很多错误。

public class Reader {

    In input = new In("file:world_cities.txt");
    private static City cityInfo;

    public static void main(String[] args) {
        // open file
        In input = new In("world_cities.txt");
        input = new In("world_cities.txt");
        try {
        // write output to file
        FileWriter fw = new FileWriter("cities_out.txt");
        PrintWriter pw = new PrintWriter(fw);

        int line = 0;

        // iterate through all lines in the file
        while (line < 47913) {

            // read line
            String cityLine = input.readLine();

            // create array list
            ArrayList<String> cityList = new ArrayList<String>(Arrays.asList(cityLine.split(",")));

            // add line to array list
            cityList.add(cityLine);

            // increase counter
            line += 1;      

            // create instance
            cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), cityList.get(4), cityList.get(5));
            System.out.println(cityInfo);

            // print output to file 
            pw.println(cityInfo); 
        }

        // close file
        pw.close();
        }

        // what is printed when there is an error when saving to file
        catch (Exception e) {
            System.out.println("ERROR!");
        }

        // close the file
        input.close();
    }
}

最佳答案

如果按如下方式声明列表,则可以将任何引用类型的实例放入其中:

    List<Object> list = new ArrayList<>();

但缺点是,当你从列表中获取一个元素时,该元素的静态类型将为 Object ,您需要将其类型转换为您需要的类型。

另请注意,您不能输入 intdouble进入List 。基本类型不是引用类型,并且List API 要求元素是引用类型的实例。需要使用对应的包装类型;即IntegerDouble .

<小时/>

查看更多代码,我发现了这一点:

ArrayList<String> cityList = 
    new ArrayList<String>(Arrays.asList(cityLine.split(",")));

如果将列表更改为 List<Object>其中对象是 IntegerDouble ,您将无法像这样构建列表。

事实上,我越看这个,就越觉得你根本不需要列表。你应该做这样的事情:

    // read line
    String cityLine = input.readLine();

    String[] parts = cityLine.split(",");
    // increase counter
    line += 1;      

    // create instance
    cityInfo = new City(parts[0], parts[1], parts[2], 
                        Integer.parseInt(parts[3]),
                        Double.parseDouble(parts[4]),
                        Double.parseDouble(parts[5]));

注意:没有List根本就在那里!!

关于java - 在Java中创建多种数据类型的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570285/

相关文章:

java - 将 "Selector modules"与 DataMovement SDK MarkLogic 结合使用 [Java] [MarkLogic] [dmsdk] [data-movement-sdk][ml-java-api]

java - 仅当实体确实发生更改并将更新时才设置changedby字段。

java - 如何使用 Java Mail 发送 iCal session 请求并接收响应

java - 如何防止对象 ArrayList 中出现重复

Java - 验证 arrayList 中是否已存在行号,

java - 如何实现List<T>到T[]转换的通用方法?

java - 使用 maven2 删除嵌套的测试类

java p2p 视频库?

ASP.Net Core - 获取帖子表单的所有数据

java - 异常ArrayIndexOutOfBoundsException错误