简体   繁体   English

从数组中删除重复项并写入文本文件

[英]remove duplicates from array and write to text file

Iv created an array now i have to remove the duplicates from the array and write the duplicates in to a txt file and put this random numbers in order higher to lower, but if someone could at least show me how to get those duplicates integers out of the array and put the duplicates in variable that would be perfect already. Iv创建了一个数组现在我必须从数组中删除重复项并将重复项写入txt文件并将这些随机数放入更高的位置,但是如果有人至少可以告诉我如何获取那些重复的整数数组并将重复项放入已经完美的变量中。 This is my code. 这是我的代码。 Thank you! 谢谢! No array list plz. 没有数组列表plz。

import java.util.Random;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
public class MainProg extends GenKeys {

    public static void main(String[] args) {
        //int x = random();
        try {

            BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));


            for (int z = 0; z < 500; z++) {
                int x = random();
                out.write(x + System.getProperty("line.separator"));
            }
            out.close();

            readFromfile();
        } catch (IOException e) {
            System.out.print(e);
        }
    }

    public static void readFromfile() throws IOException {
        int[] numbers = new int[500];
        int result, searchValue;

        int index = 0;

        // Open the file.
        File file = new File("file.txt");
        Scanner inputFile = new Scanner(file);
        int w = 0;
        for (int i = 0; i < numbers.length; i++) {
            if (i == 0 || numbers[i] != numbers[i - 1]) {
                numbers[w++] = numbers[i];

                while (inputFile.hasNextInt() && index < numbers.length) {

                    numbers[index] = inputFile.nextInt();
                    System.out.println(numbers[index]);
                    index++;
                }
            }
        }

        // Close the file.
        inputFile.close();
    }
}

GenKeys Method GenKeys方法

 import java.util.Random;

public class GenKeys {

static int x;


public static int random(){
for (int i = 0; i < 250; i++) {

 x = (int) (Math.random() * 250);

}

return x;




}
}

Instead of storing integers in an array store them inside a HashSet<Integer> . 而不是在数组中存储整数将它们存储在HashSet<Integer>

Replace int[] numbers = new int[500]; 替换int[] numbers = new int[500];

with Set<Integer> numbers = new HashSet<Integer>(); 使用Set<Integer> numbers = new HashSet<Integer>();

and replace all the methods called on numbers with the methods available for Set . 并使用Set可用的方法替换所有调用numbers的方法。

Alright, first of all you have some strange code, and whilst it may work, it's still very strange. 好吧,首先你有一些奇怪的代码,虽然它可能有用,但它仍然很奇怪。 So let's start by cleaning up your looping code: 那么让我们从清理循环代码开始:

First off you have too many counting variables. 首先,你有太多的计数变量。 I see index, w, and i. 我看到索引,w和i。 You don't need that many. 你不需要那么多。 Second, if you we look at these 2 lines of code of yours: 其次,如果你看看你的这两行代码:

if (i == 0 || numbers[i] != numbers[i - 1]) {
                numbers[w++] = numbers[i];

We then must ask ourselves what is the purpose of the first part of that if statement? 那么我们必须问自己,if语句的第一部分的目的是什么? If i == 0 Well that will only be true on the first time through, and so w will also be 0. so then we get to the inner part of the the if and you set numbers[0] = numbers[0] not needed, so we can remove the i == 0 part of your if statement because it is gaining us nothing. 如果i == 0那么只有在第一次通过时才会成立,因此w也将为0.所以我们到达if的内部部分并且你设置numbers[0] = numbers[0]没有需要,所以我们可以删除你的if语句的i == 0部分因为它没有获得任何东西。 Then continuing on, you go into the while loop and through all the input file and add all the random numbers to your numbers array. 然后继续,你进入while循环并通过所有输入文件,并将所有随机数添加到数字数组。 Then you go back to the for loop and i is now 1, but you'll never go though the while loop again because index is too big, and instead you're doing some strangeness with your w and i and numbers , it appears to me that you're setting numbers[1] = numbers[1], numbers[2] = numbers[2] ect, except in the case where you happen to get two random numbers that were the same in a row, in which case you then start doing numbers[3] = numbers[4] ect. 然后你回到for循环, i现在是1,但你永远不会再通过while循环,因为索引太大了,相反,你对你的winumbers做了一些奇怪,它似乎我正在设置numbers[1] = numbers[1], numbers[2] = numbers[2] ,除非您碰巧得到两个连续相同的随机数,在这种情况下然后你开始做numbers[3] = numbers[4] it's all very strange and I'm sure incorrect. 这一切都很奇怪,我肯定不对。

So what you probably want is something a bit more like: 所以你可能想要的东西更像是:

index = 0;
while (inputFile.hasNextInt() && index < numbers.length) {   
    numbers[index] = inputFile.nextInt();
    System.out.println(numbers[index]);
    index++;
}

That will populate your numbers array with only unique numbers. 这将使用唯一的数字填充您的数字数组。 Then follow it up with a sort to order the numbers. 然后按顺序跟进,以订购数字。

Arrays.sort(numbers);

Then go through and remove duplicates. 然后浏览并删除重复项。

for (int i = 0, j = 0; i < numbers.length; i = j > i ? j : i + 1) {
    boolean duplicate = false;
    j = i;
    do {
       if (j + 1 >= numbers.length) {
           break;
       }
       duplicate = false;
       if (numbers[i] == numbers[j + 1]) {
           numbers[j + 1] = -1; // setting it to negative 1 so we know it's "removed"
           duplicate = true;
           j++;
       }
    } while (duplicate);
}

Now you can either create a new array and read through populating only positive values...or just use the existing array to print out the remaining numbers. 现在,您可以创建一个新数组并通过仅填充正值来读取...或者只使用现有数组打印出剩余的数字。

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

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