在数组中读取和存储唯一值(JAVA)

时间:2011-04-27 16:33:05

标签: java arrays unique

所以我在java作业中遇到了问题。任务是编写一个读取十个数字的程序,只显示不同的数字以及不同值的数量。到目前为止我得到的是......

import java.util.Scanner;
import java.util.Collection;

public class Exercise06_05 {
    public static void main(String[]args){
        Scanner input = new Scanner(System.in);
        int[] list = new int[10];//create my new 10 slot array
        //i really want a variable length array but alas

        for (int i = 0; i < 10; i++){//get and check the ten input variables
            System.out.println("Enter an Integer");//ask for input
            int integer = input.nextInt();//assign the input to a temp variable
            if (isUnique(list, integer)){//check if the temp is unique to the array
                list[i] = integer;//if so assign it
            }
        }



       String output = "";
       int j = 0;
       for (j = 0; j < list.length; j++){
           if(list[j] != 0){//this is where the error accours
                   output += (list[j] + " ");
           }else
               break;//this break ensures J doesn't get any higher
           //so that i can plug that in for the number of distinct variables
       }
       System.out.println("The number of distinct numbers is " + j);
       System.out.println(output);//print output, and the number of distinct values


    }
           public static boolean isUnique(int [] arry, int a){// my masterpiece of a method
             for (int i = 0; i < (10);){
              if (arry [i] == a){//check box
                return false;//not unique
             } else if (i == (arry.length - 1)){//we done yet?
                return true;//if so, return that it's unique
             }else//if we're not done increment the box
               i++;//there that is
             } return false;//now put this here just to safeguard


       }

   }

除非用户连续输入两个相同的整数,然后再输入1,否则它会正常工作。会发生什么是程序不存储第二个int,数组保持为零,然后在创建输出部分失败。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

好吧这有点不同,它不是回答您的问题,您已经这样做了,它更关注您的解决方案。您首先阅读数字列表,然后对这些数字进行操作。 这是另一个片段:

loop = 0;

/**Start Reading your numbers from stdin or anything**/
while(There are numbers to read){

    int number = read_number(), counter = 0, found = 0;

    while(counter <= loop){
        if(a[counter] == number){ 
            found = 1; // you found a match for this number break here 
            break;
        }

        counter++;  
    }

    if(found == 0){
        /* We did not find our number in array*/
        a[counter] = number;
    }
}

这只是一次读取而只循环一次。

答案 1 :(得分:0)

您的问题是您尝试输出的值多于数组中的值。

执行int[] list = new int[10]意味着list.length将始终为10.

从数组切换到List,或跟踪插入的数字。

答案 2 :(得分:0)

即使未存储的值,i也会增加1。

一个解决方案是:

if (isUnique(list, integer)){//check if the temp is unique to the array
            list[i] = integer;//if so assign it
        }
else
{
    list[i] = 0;
}

将在单元格中存储0,否则单元格中没有任何内容(甚至不是0),程序崩溃。

相反,如果你想为单元格获取另一个值,请将i减1,而不是将0分配给单元格。

答案 3 :(得分:0)

嗯,首先,如果您获得的数字是唯一的,则将其插入数组中。如果它不是唯一的,则不要将其插入到数组中,但仍然可以推进数组索引。因此,您将保留初始化默认值为零。

这有两个问题。

  1. 无法将“重复值”与“零”区分开来。如果零不是合法值,那么没关系。如果零是合法的,这将不起作用。

  2. 更基本的是,当你在输出上循环遍历数组时,当你达到第一个零时退出。因此,如果用户输入了1,2,4,2,3,4,那么你就可以用1,2,4,0,3,0来填充数组。然后,当您显示输出时,您将要编写1,2,4,请查看零并退出,并声明有3个唯一值。你永远不会达到3。

  3. 我不知道他们已经教会了你有多少可用的数据结构。更好的解决方案是使用ArrayList而不是数组,然后仅在传入值唯一时添加到ArrayList。如果你还没有了解到这一点,那么另一个想法是在你继续进行时保持对唯一值数量的反击。当您获得重复值时,不要在数组中插入,也不要增加计数器。也就是说,有一个计数器是arrray中的位置,它与唯一值的数量相同。有另一个计数器,它是读取的输入值的数量。一旦看到第一个副本,这将大于数组的位置。

    On,在一个细节点上,与你的问题没有直接关系:在你的isUnique函数中,为什么循环到一个硬编码的十,然后有一个单独的IF语句来测试数组长度并在你打破时到达终点?如果您编码,它会更简单,更容易阅读:

    public static boolean isUnique(int [] arry, int a)
    {
      for (int i = 0; i < arry.length)
      {
        if (arry [i] == a)
        {
          return false; //not unique
        }
      }
      return true; // we made it through without finding a dup, must be unique
    }
    

    或者,如上所述,您是否有一个单独的变量来说明实际填充了多少数组:

    public static boolean isUnique(int [] arry, int filled, int a)
    {
      // filled should always be <=arry.length, but we can check both just to be safe
      for (int i = 0; i < arry.length && i<filled)
      {
        if (arry [i] == a)
        {
          return false; //not unique
        }
      }
      return true; // we made it through without finding a dup, must be unique
    }