从数组列表中提取唯一值 [英] Extraction of unique values form a array list

查看:241
本文介绍了从数组列表中提取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Java编程我还是很陌生,我想制作一个程序,该程序可以从文件中打印出一些值.我想从包含大量重复数字的文件中导入数组列表.该程序应仅打印一组唯一的编号.

I'm pretty new to programming in Java and I want to make a program that will print out some values from a file. I want to import a array list from a file which contains a large set of repeated numbers. The program should print out only one unique number of set.

例如,数组包含以下数字:

For example, the array contains these numbers:

0,0,0,0,2,2,2,2,2,3,3,3,3,3,5,5,5,5,8,8,10,10,2,2,2,3,3,7,7

我应该从中得到的是:

0,2,3,5,8,10,2,3,7

如果数组中不包含整数,但包含浮点数,则需要这样做:

The same would be needed if the array wasn't containing integers, but floating point numbers:

0.23, 0.23, 0.23, 0.23, 1.89, 1.89, 1.89, 1.89, 1.89, 2.56, 2.56, 2.56, 2.56, 2.56, 3.13, 3.13

输出应为:

0.23, 1.89, 2.56, 3.13

有人对我应该如何建立循环有一些建议吗?

Has anyone some suggestions on how should I build the loop?

预先感谢

推荐答案

代码:

    int[] input = new int[]{0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 10, 10, 2, 2, 2, 3, 3, 7, 7};
    int current = input[0];
    boolean found = false;

    for (int i = 1; i < input.length; i++) {
        if (current == input[i] && !found) {
            found = true;
        } else if (current != input[i]) {
            System.out.print(" " + current);
            current = input[i];
            found = false;
        }
    }
    System.out.print(" " + current);

输出:

0 2 3 5 8 10 2 3 

说明:

您通过设置数组的第一个值来设置电流 在循环内部,如果当前值等于每个元素 并且标志fount为false,表示未看到重复项,并且 您现在已经看到了,所以找到变量的标志有 设置为true,表示已找到重复的内容或看到重复的内容 没有.

you set current with first value of array after that you go through the loop.Inside the loop, if the current value equal to each elements and flag fount is false which mean duplicate have been not seen and you it has been seen right now, so flag which is found variable has been set to true which shows duplicate has been found or seen an do nothing.

对于else部分,如果数组的元素不等于当前 变量意味着没有重复,而正确的当前变量表明 包含在控制台上以前见过的重复项.更新 具有新重复项的可变电流并将设置标志设置为false 节目还没看过新看过的重复.继续这样做,就在最后 遍历循环后,复制变量.

For the else part, if the element of array not equal to current variable means there is not duplicate, and right current variable that contains the before seen duplicate at the console. Update the variable current with new duplicate and set flag found to false which shows have not seen new seen duplicate. Keep doing this and right last duplicate variable when you are done traversing the loop.

这篇关于从数组列表中提取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆