简体   繁体   中英

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. This is my code. Thank you! No array list 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

 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> .

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

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

and replace all the methods called on numbers with the methods available for Set .

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. 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 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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