在数组列表中替换

时间:2014-11-10 22:06:30

标签: java arrays

我必须在1到20之间列出10个唯一号码,但在存储号码之前,程序应该检查号码是否在列表中。如果该号码已在列表中,则应生成新号码。此外,必须计算替换的数量。

这是我到目前为止所做的:

public static void main(String[] args)
{
    int[] arrayA = {16, 14, 20, 3, 6, 3, 9, 1, 11, 2};

    System.out.print("List: ");
    for(int w = 0; w < arrayA.length; w++)
    {
        System.out.print(arrayA[w] + " ");
    }
}

如您所见,列表中有两个&#34; 3&#34; s,我必须输出相同的列表,但更改其中一个&#34; 3&#34; s。另外,它必须被计算在内。

4 个答案:

答案 0 :(得分:1)

这不难做到,但改变其中一个三分是什么意思?

你可以在你的for循环之外添加一个布尔标志,告诉你是否遇到了3或者是3的索引。

尝试这样的事情:

boolean changedThree = false;
int threeIndex = -1;
for(int i = 0; i < arrayA.length; i++){
   if(arrayA[i] == 3 && !changedThree){
      arrayA[i] = 4;
      threeIndex = i;
      changedThree = true;
   }
   System.out.println(arrayA[i] + " ");
}

我不确定是否能抓住您需要的信息,但希望可以帮助您朝着正确的方向前进。如果您有疑问,请告诉我。

修改

为避免出现任何重复值,建议您创建一个数组列表,并为其添加唯一值。然后,您可以使用ArrayList.contains()方法查看值是否已存在。所以,我建议您将代码更改为:

ArrayList<int> usedCharacters = new ArrayList<int>();
int changedCounter = 0;
Random rand = new Random();
for(int i = 0; i < arrayA.length; i++){
   if(!usedCharacters.contains(arrayA[i])){ // If we haven't used this number yet
      usedCharacters.add(arrayA[i]);
   } else{
      // Generate a new number - make sure we aren't creating a duplicate
      int temp = rand.nextInt(20) + 1;
      while(usedCharacters.contains(temp)){
         temp = rand.nextInt(20) + 1;
      }
      // Assign new variable, increment counter
      arrayA[i] = temp;
      changedCounter++;
   }
}

如果您不熟悉random.nextInt()方法,请阅读t h

答案 1 :(得分:0)

所以,如果我理解正确你必须保存arrayA,对吧? 如果是这种情况,您可以创建一个新数组targetArray,您可以将其保存到数字,然后使用for循环进行检查(如果已添加),如果是,则可以生成新的随机数。 结果看起来像这样:

public static void main(String[] args) {
    int[] arrayA = {16, 14, 20, 3, 6, 3, 9, 1, 11, 2};
    int[] targetArray = new int[10];
    int numbersReplaced = 0;

    System.out.print("List: ");
    for (int i = 0; i < arrayA.length; i++) {
        for (int j = 0; j < targetArray.length; j++) {
            if (arrayA[i] == targetArray[j]) {
                targetArray[j] = (int)(Math.random() * 100);
                numbersReplaced++;
            } else {
                targetArray[j] = arrayA[i];
            }
        }
    }
    System.out.println("Numbers replaced: " + numbersReplaced);
}

希望有所帮助

答案 2 :(得分:0)

注意:这是我更喜欢提供指导答案的问题,而不仅仅是粘贴代码。

您可以向后看数组,查看前面的子列表。如果它包含当前号码,则用新号码替换。

获取类似Arrays.asList(array).subList(0,i)的子列表,然后使用.contains()。

您找到要添加的数字的逻辑取决于很多东西,但最简单的方法是,您可能需要首先遍历数组才能找到“可用”数字 - 并将它们存储在新列表中。每次需要更换时,从该列表中拉出一个新号码。

编辑:正如评论中所建议的,您也可以在这里使用Java Set。请参阅Set docs

答案 3 :(得分:0)

您可以使用递归来实现结果。 这将保持循环,直到所有值都是唯一的

private void removeDoubles(int[] arr) {
    for(int i = 0; i < arr.length; i++)
    {
        // iterate over the same list
        for(int j = 0; j < arr.length; j++) {
            // Now if both indexes are different, but the values are the same, you generate a new random and repeat the process
            if(j != i && arr[i] == arr[j]) {
                // Generate new random
                arr[j] = random.nextInt(20);
                // Repeat
                removeDoubles(arr);
            }
        }
    }
}