Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从/到文件读取和写入整数_Java_Integer_Set - Fatal编程技术网

在Java中从/到文件读取和写入整数

在Java中从/到文件读取和写入整数,java,integer,set,Java,Integer,Set,我刚刚创建了一个将整数从文件读入集合的应用程序,我有一个问题,当我的输入文件中有数字-5 5 2 8 9 1 4 55 70时,它会以这种方式将数字保存到集合中-[1,2,4,5,70,55,8,9],为什么会这样?我希望它能避免口是心非——这没关系,但我想先保存数字 Set<Integer> zoznam = new HashSet(); int index = 0; FileReader fr; fr = new FileReader(fileNa

我刚刚创建了一个将整数从文件读入集合的应用程序,我有一个问题,当我的输入文件中有数字-5 5 2 8 9 1 4 55 70时,它会以这种方式将数字保存到集合中-[1,2,4,5,70,55,8,9],为什么会这样?我希望它能避免口是心非——这没关系,但我想先保存数字

    Set<Integer> zoznam = new HashSet();
    int index = 0;
    FileReader fr;
    fr = new FileReader(fileName);
    String line;
    BufferedReader br = new BufferedReader(fr);
    int i = 0;

    while ((line = br.readLine()) != null) {

        System.out.println(line);
        String[] items = line.split(" ");
        int[] c = new int[items.length];
        for (int q = 0; q < items.length; q++) {
            c[q] = Integer.parseInt(items[q]);
            zoznam.add(c[q]);
        }
    }
    return zoznam;
}
Set zoznam=new HashSet();
int指数=0;
文件阅读器fr;
fr=新文件读取器(文件名);
弦线;
BufferedReader br=新的BufferedReader(fr);
int i=0;
而((line=br.readLine())!=null){
系统输出打印项次(行);
String[]items=line.split(“”);
int[]c=新的int[items.length];
对于(int q=0;q
A
Set
通常不保证任何订单。如果要保持顺序,需要使用专门的实现,例如a,则保留插入顺序。

谢谢,伙计!!这对我帮助很大lot@DebosmitRay
TreeSet
是一个
SortedSet
-它将对插入的项目进行排序,并根据它们的自然顺序显示它们。对于OP(5 5 2 8 9 1 4 55 70)给出的输入,输出为1 2 4 5 8 9 55 70。相反,
LinkedHashSet
保留插入顺序。非常抱歉。我读错了。我认为OP希望元素有序,而不是保持有序。很抱歉,穆雷尼克。