简体   繁体   English

避免打印重复

[英]Avoid to print duplicates

I wish to generate result that displaying different distinct/unique combinations of numbers. 我希望产生的结果显示不同的独特数字组合。

For eg: if number is 4 digit ie: 4789 then output will be in format of 478,789,489,479 (in increasing order only.) My code is: 例如:如果数字是4位数字,即:4789,则输出将为478,789,489,479(仅以递增顺序)。我的代码是:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;

public class Permutation{
    static void permute(int[] a, int k) {
        int temp;
        if (k == a.length) {
            for (int i = 0; i < a.length - 1; i++) {
                for (int j = i + 1; j < a.length - 1; j++) {
                     if (a[i] > a[j]) {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                     }
                  }
            System.out.print(a[i]);
            }
        System.out.println();
        } else {
            int tempLength = (a.length);
            for (int i = k; i < tempLength; i++) {
                temp = a[k];
                a[k] = a[i];
                a[i] = temp;
                permute(a, k + 1);
            }
        }
    }

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the length of list: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];
        System.out.println("---------");
        for (int i = 0; i < N; i++)
            {
        sequence[i] = sc.nextInt();
            }
        System.out.println("The original sequence is: ");
        for (int i = 0; i < N; i++){
        System.out.print(sequence[i] + " ");
        System.out.println("\nThe permuted sequences are: ");
        permute(sequence, 0);
        sc.close();
    }}

Output of above program is: 上面程序的输出是:

Enter the length of list: 4
---------
1
2
7
8
The original sequence is: 
1 2 7 8 

The permuted sequences are: 

127,128,128,127,178,127,127,128,128,127,178,127,
127,128,128,127,178,127,278,127,127,128,178,127

So this result displays some duplicates elements. 因此,此结果显示一些重复元素。 I want to avoid displaying such duplicate elements in array. 我想避免在数组中显示这样的重复元素。 In above code I'm expecting as output:127,128,178,278 Help, thank you in advance. 在上面的代码中,我期望输出为:127,128,178,278帮助,在此先感谢您。

You can use StringBuilder : 您可以使用StringBuilder

private static String[] getDigitsArray(String str) {
    StringBuilder sb = new StringBuilder(str);
    sb.deleteCharAt(3).append(",")
            .append(new StringBuilder(str).deleteCharAt(0)).append(",")
            .append(new StringBuilder(str).deleteCharAt(1)).append(",")
            .append(new StringBuilder(str).deleteCharAt(2));
    return sb.toString().split(",");
}

Simple use: 使用简单:

String[] array = getDigitsArray("4789");
System.out.println("Result: "+Arrays.toString(array));

And it is the result: 结果是:

Result: [478, 789, 489, 479]

For every 4 digits string you can use the code above, it will return an array which has for elements inside, and then you can do what you want with. 对于每4位数字的string您可以使用上面的代码,它将返回一个array ,其中包含用于元素的array ,然后您可以执行所需的操作。

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

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