简体   繁体   English

二维数组列表和输入/排序

[英]two dimensional arraylists and input/sorting

I am trying to add all the doubles between two <end> instances into an arraylist within an arraylist (at each index of <end> have the doubles between the <end> s, and so forth) 我想两者之间添加所有双打<end>实例插入一个ArrayList一个ArrayList内(的每个索引处<end>具有之间双打<end> S,等等)

ArrayList<ArrayList<Double>> myArr = new ArrayList<ArrayList<Double>>();
int j = 0;
int nLine = 0;

    while(scan.hasNext()) {
        String line = scan.next();

        //System.out.println(line);
        if(!line.equals("<end>")) {
            nLine = Double.valueOf(line);
            ArrayList<Double> row = new ArrayList<Double>();
            myArr.add(row);


            row.add(j, nLine);
            j=j++;

        } else {
            j=j++;
        }
    }

As it stands now the code is putting in a single double in a an array (as opposed to all of the ones between statements; thus the output looks like this: [[1.4], [3], [15], [3.2], etc. etc. 就目前而言,代码将单个double放入数组中(而不是语句之间的所有double;因此输出看起来像这样:[[1.4],[3],[15],[3.2]等等等等

where I want it to look like this: [[1.4, 3, 15, 3.2], [5, 13.4], [954.3, etc.... 我希望它看起来像这样:[[1.4,3,15,3.2],[5,13.4],[954.3等...

The file it is scanning is essentially: 它正在扫描的文件实质上是:

<end>
1.4
3
15
3.2
<end>
5
13.4
<end>
954.3
43 etc. etc. (infinitely)

My goal (eventually) is to tally how many doubles are in each arrayList index and make sure none of the doubles are exactly the same, and to make sure each of the row arrays have no more than 10 values in them. 我的目标(最终)是确定每个arrayList索引中有多少个双精度数,并确保没有一个完全相同的双精度数,并确保每个行数组中的不超过10个值。 So I have been stuck and any help is appreciated. 所以我被困住了,任何帮助我们都感激不尽。

Thanks for any help. 谢谢你的帮助。

ArrayList<ArrayList<Double>> myArr = new ArrayList<ArrayList<Double>>();
int nLine = 0;

ArrayList<Double> currArr = null;

while(scan.hasNext()) {
    String line = scan.next();

    if(!line.equals("<end>")) {
        nLine = Integer.valueOf(line);

        currArr.add(nLine);
    } else {
        if(currArr!=null) myArr.add(currArr);

        currArr = new ArrayList<Double>();
    }
}

if(currArr!=null) myArr.add(currArr);

In the middle of the code you're using Integer instead of Double . 在代码中间,您使用Integer而不是Double Not sure why so I left it. 不知道为什么,所以我离开了。 Code assumes the input always starts with <end> . 代码假定输入始终以<end>开头。

You could use a HashSet as a way to keep track of the duplicates (or better yet use an ArrayList of Sets instead to avoid the extra data structure) 您可以使用HashSet来跟踪重复项(或者更好的方法是使用Set的ArrayList来避免额外的数据结构)

Here's an example of generating a random # of ArrayLists without dups: 这是一个生成不随机的ArrayList随机数的示例:

ArrayList<ArrayList<Integer>> myArr = new ArrayList<ArrayList<Integer>>();      
HashSet<Integer> duplicates = new HashSet<Integer>();
Random random = new Random();

for(int x=0; x<3; x++) {
    ArrayList<Integer> row = new ArrayList<Integer>();
    myArr.add(row);
    for(int y=0; y<3; y++) {
        int randomInt = random.nextInt(100);
        if(!duplicates.contains(randomInt)) {
            row.add(0,randomInt);
            duplicates.add(randomInt);
        }
    }
}

for(int i=0;i<myArr.size();i++)
{
    System.out.println(myArr.get(i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM