0

コレクションを使用せずに、整数配列から重複する整数要素を削除するJavaプログラムを作成しましたが、プログラムは整数要素を1つだけ削除し、他の整数要素は残ります。

以下のコア Java プログラムで重複した整数要素を削除する方法を教えてください。以下のコア Java プログラムでは、重複した整数要素を削除する必要があります 5

提供されたヘルプに感謝します。

以下は Java コードです。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DeleteElementFromArray {

    static int[] a = {5,1,2,3,4,5,7,8,9,10};
    static int[] b = new int[10];
    static int i, k, f, j = 0;
    static int l = a.length;

    void DeletElementInt() {
         for (i = 0; i < l; i++) {
             if (i != k) {
                   if (i < k) {
                       b[i] = a[i];
                   } else{                    
                         b[i - 1] = a[i];
                   }  
             }
         }
     }       



    public static void main(String[] args) {
          DeleteElementFromArray d = new DeleteElementFromArray();
          System.out.println("Array Elements are ");
          for (i = 0; i < l; i++){
            System.out.println(a[i]); 
          }
          InputStreamReader is = new InputStreamReader(System.in);
          BufferedReader br = new BufferedReader(is);
          System.out.println("Enter the Element for Delete");
          try {
             String s = br.readLine();
             f = Integer.parseInt(s);
              for (i = 0; i < l; i++) {
                if (f == a[i]) {
                  System.out.println("Delete Element found from given array");
                  k = i;
                  j++;
                  d.DeletElementInt();
                }
              }
                 l = l - 1;
                 System.out.println("New Array ");
                 for (i = 0; i < l; i++) 
                 {
                      System.out.println(b[i]);
                 }
                 if (j == 0) {
                   System.out.println("Entered Element does not found from given array");
                 }
          } catch (IOException e) {
                System.out.println(e);
          }
    }
}


//output
/*
Array Elements are 
5
1
2
3
4
5
7
8
9
10
Enter the Element for Delete
5
Delete Element found from given array
New Array 
1
2
3
4
5
7
8
9
10
*/
4

7 に答える 7

2

固定コードは次のとおりです。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DelElem {

    static int[] a = {5,1,2,3,4,5,7,8,9,10};
    static int[] b = new int[10];
    static int f, i, k, j = 0;
    static int l = a.length;

    static void DeleteElementInt(int elementToDelete) {
        j = 0;
        for (int i = 0; i < l; i++)
            if (a[i] != elementToDelete)
                b[i - j] = a[i];
            else
                ++j;
    }



    public static void main(String[] args) {
        System.out.println("Array elements are:");
        for (i = 0; i < a.length; i++)
            System.out.println(a[i]);
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(is);
        System.out.print("Enter the element to be deleted: ");
        try {
            String s = br.readLine();
            f = Integer.parseInt(s);
            DeleteElementInt(f);
            System.out.println("New array:");
            for (i = 0; i < l - j; i++)
                System.out.println(b[i]);
            if (j == 0)
                System.out.println("Entered element was not found in the given array");
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}


//output
/*
Array elements are:
5
1
2
3
4
5
7
8
9
10
Enter the element to be deleted: 5
New array:
1
2
3
4
7
8
9
10
*/
于 2013-04-19T10:59:58.843 に答える
0
import java.util.Scanner;

public class RemoveAllOccurences{

    static int[] removeAll(int[] a,int n){
        int[] dupl = new int[a.length];
        for(int i = 0;i < dupl.length;i++){
            dupl[i] = -999;
        }
        int index = 0;
        //looping over,finding all occurrences of n and creating new array that does not contain n.
        for(int i = 0;i < a.length;i++){
            if(a[i] != n){
                dupl[index++] = a[i];
            }
        }

        //returning array with all duplicates removed.
        return dupl;
    }

    public static void main(String[] args) {

        int[] a = {3,5,5,5,3,6,5,3,3};
        int numberToRemove;

        System.out.println("the array values are:");
        for(int i:a){
            System.out.print(a[i]+"\t");
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("\nenter the number for which all occurences need to be deleted:");

        numberToRemove = sc.nextInt();

        int[] b = removeAll(a,numberToRemove);

        System.out.println("After removing all occurences of "+numberToRemove);
        for(int i:b){
            if(i != -999)
                System.out.print(i+"\t");
        }

    }
}
于 2015-03-30T15:56:28.143 に答える
0
public static void main(String[] args) {
    int a[]={1,4,3,2,6,5,7,3,5,4,2};
    int b[]=new int[a.length];
    Arrays.sort(a);
    int j=0;
    for(int i=0;i<a.length;i++){
        while(i<a.length-1 && a[i]==a[i+1]){
            a[i]=999;  // This can be any tag which you are not going to have in your array
            i++;
        }
        if(a[i]!=999)
            b[j++]=a[i];
    }

    System.out.println(b);
}
于 2019-11-23T07:12:17.060 に答える