English 中文(简体)
核对重复分类账的一个层面阵列 Java
原标题:Checking one dimensional array for duplicate integers Java
  • 时间:2011-03-19 22:30:08
  •  标签:
  • java

hey guys, right i have an array of 50 random integers and i have to check if any of them are the same, here is my code so far it only checks ajacent indexs

for (int i =0; i < 50; i++)
{
    System.out.print("Student" + i + ": "   );

    customers[i] = (int)((Math.random()*10000)%10+1);
    System.out.print(" " +customers[i]+ "
");



    if( duplicate == customers[i])
    {
        System.out.println("yup");
    }
    duplicate = customers[i];
}
问题回答

排第一。 然后,你只能检查下一个指数。 如果是这样的话,就会中断。


Okay, I hate ridiculous limitations. If you want, you can do it like this without using a sort:

import java.util.ArrayList;
import java.lang.Math;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        Integer currentValue = 0;

        int i = 0;
        int limit = 20;

        for(i = 0; i < limit; i++) {
            list.add((int)(Math.random() * 100));            
        }      

        for(i = 0; i < limit; i++) {
            currentValue = list.get(i);
            list.set(i, -1);
            if(list.contains(currentValue)) {
                System.out.println("yup:" + currentValue);
                return;
            } else {
                list.set(i, currentValue);
            }
        }

        System.out.println("No duplicates!");
        return;
    }
}

它是否有效? 页: 1

它是否发挥作用? 是的。

我认为,如果你在履行职能时必须公正使用,那么你必须作两点选择:

for (int i =0; i < 50; i++)
{
    customers[i] = (int)((Math.random()*10000)%10+1);

    for ( int j = 0; j < i; j++)
    {
        if( customers[j] == customers[i])
        {
            // duplicated entry. do what you want
            System.out.println("yup");
        }
    }
}

页: 1 http://www.exampledepot.com/egs/java.util/coll_SortArray.html” rel=“nofollow” http://www.exampledepot.com/egs/java.util/coll_SortArray.html)以分类数据,然后核对复制件。

贵方使用价值0-10,以便你能够节省在豆类阵列中的贵重价值,从而核实这是否重复:

boolean[] checker = new boolean[11];        
for (int i =0; i < 50; i++) {
    customers[i] = (int)((Math.random()*10000)%10+1);
    if (checker[customers[i]]) {
        System.out.println("yup"); //duplication
    } else {
        checker[customers[i]] = true;
    }
}

You can use the below code if you are working with Integer array:

public class DuplicateInteger {

    private static int countDuplicate;

    public static int[] getDuplicateIntegers(int[] integerArray){
        int duplicateIntegers[] = new int[integerArray.length];
        countDuplicate = 0;
        for(int i=0;i<integerArray.length;i++){
            for(int j=i+1;j<integerArray.length;j++){
                int replicaTest = 0;
                if(integerArray[i]==integerArray[j]){
                    for(int k=0;k<countDuplicate;k++){
                        if(duplicateIntegers[k]==integerArray[i]){
                            replicaTest = 1;
                        }
                    }
                    if(replicaTest==0){
                        duplicateIntegers[countDuplicate] = integerArray[i];
                        countDuplicate++;
                    }
                }
            }
        }
        return duplicateIntegers;
    }

    public static void printDuplicateIntegers(int[] duplicateIntegers){
        System.out.println("Duplicate Integers:");
        System.out.println("-------------------");
        for(int i=0;i<countDuplicate;i++){
            System.out.println(duplicateIntegers[i]);
        }   
    }

    public static void main(String[] args){
        int numberArray[] = {1, 2, 3, 4, 5, 6, 7, 1, 3, 5, 7};
        printDuplicateIntegers(getDuplicateIntegers(numberArray));
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签