Java 将重复的数字移动到数组的末尾

Java 将重复的数字移动到数组的末尾,java,methods,Java,Methods,我需要做一个计算Yahtzee中smallStraight的方法(smallStraight意味着你有4个骰子,增加了一个..例如,1,2,3,4,6是一个小直线)。现在,我正试图克服这样一个障碍:当对数组进行排序时,可能会出现重复的数字。 例如,如果滚动然后排序,可能会得到1、2、2、3、4。现在我基本上需要删除数组末尾的第二个2。这是我的密码。注意,对于嵌套的四个循环,这显然不起作用。我只是想知道最好的办法 public int setSmallStraight(int[] die) {

我需要做一个计算Yahtzee中smallStraight的方法(smallStraight意味着你有4个骰子,增加了一个..例如,
1,2,3,4,6
是一个小直线)。现在,我正试图克服这样一个障碍:当对数组进行排序时,可能会出现重复的数字。
例如,如果滚动然后排序,可能会得到
1、2、2、3、4
。现在我基本上需要删除数组末尾的第二个2。这是我的密码。注意,对于嵌套的四个循环,这显然不起作用。我只是想知道最好的办法

public int setSmallStraight(int[] die)
{
    if (!isSmallStraightUsed)
    {
      int counter = 0;
      boolean found = false;
      Arrays.sort(die);

      for (int i = 0; i < die.length - 1; i++)
      {
          if (counter == 3)
              found = true;

          if (die[i + 1] == die[i] + 1)
          {
              counter++;
          }
          else if (die[i + 1] == die[i])
          {
              continue;
          }
          else
          {
              counter = 0;
          }
      }

      if (found)
      {
         smallStraight = 30; 
      }
      else
      {
          smallStraight = 0;
      }
      return smallStraight;
    }
   else
        return 0;
   }
public int-setsmallright(int[]die)
{
如果(!已使用)
{
int计数器=0;
布尔值=false;
数组。排序(die);
对于(int i=0;i
假设数组已排序,可以使用如下算法。请阅读我的评论,希望它能清楚地解释它是如何工作的。作为预防措施,这并不是最有效的实现方式,所以如果您的数组很大,考虑性能。

// Sequentially iterate array using 2 indices: i & j
// Initially i points to 1st element, j point to 2nd element
// The assumption is there's at least 2 element in the array.
// 'end' acts as a boundary limit to which element hasn't been checked
for(int i=0,j=1,end=array.length; j<end; ) {

    // If element pointed by i & j are equal, shift element pointed
    // by j to the end. Decrement the end index so we don't test element
    // that's already shifted to the back.
    // Also in this case we don't increment i & j because after shifting we
    // want to perform the check at the same location again (j would have pointed
    // to the next element)
    if(array[i] == array[j]) {

        // This for loop shifts element at j to the back of array
        for(int k=j; k<array.length-1; k++) {
            int tmp = array[k+1];
            array[k+1] = array[k];
            array[k] = tmp;
        }

        end--;

    // if element at i and j are not equal, check the next ones
    } else {
        i++;
        j++;
    }
}
//使用2个索引顺序迭代数组:i&j
//最初i指向第一个元素,j指向第二个元素
//假设数组中至少有2个元素。
//“end”用作未检查元素的边界限制

对于(int i=0,j=1,end=array.length;j让
int计数器
计算数组中连续
+1
增加的数量怎么样?类似于:

public boolean hasSmallStraight(int[] sortedValues) {
    int counter = 0;
    for (int i=0; i<sortedValues.length-1; i++) {
        if (counter == 3) return true;

        if (sortedValues[i+1] == sortedValues[i] + 1) {
            counter++;
        } else if (sortedValues[i+1] == sortedValues[i]) {
            continue;
        } else {
            counter = 0;
        }
    }

    return counter==3;
}
公共布尔值(int[]sortedValue){
int计数器=0;

对于(int i=0;i这将适用于您:

public static void main(String[] args) {
Integer[] items = {0, 4, 2, 2, 10, 5, 5, 5, 2};
System.out.println(customSort(Arrays.asList(items)));
}

public static Collection<Integer> customSort(List<Integer> die) {
Collections.sort(die);
Stack<Integer> numbas = new Stack<Integer>();
List<Integer> dupes = new ArrayList<Integer>();
numbas.push(die.get(0));
for (int i = 1; i < die.size(); i++) {
    if (!die.get(i).equals(numbas.peek())) {
    numbas.push(die.get(i));
    } else {
    dupes.add(die.get(i));
    }
}
numbas.addAll(dupes);
return numbas;
}
添加您认为合适的错误检查和处理。

试试看

    int[] a1 = { 1, 2, 2, 3, 4 };
    int[] a2 = new int[a1.length];
    int j = 0, k = 0;
    for (int i = 0; i < a1.length - 1; i++) {
        if (a1[i + 1] == a1[j]) {
            a2[k++] = a1[i + 1];
        } else {
            a1[++j] = a1[i + 1];
        }
    }
    System.arraycopy(a2, 0, a1, j + 1, k);
    System.out.println(Arrays.toString(a1));

在您的特定情况下,自定义排序实际上有何帮助?我建议您尽量避免对数据执行此操作,除非在显示时您可以创建一个新数组(或列表)通过在排序后完全删除重复项。我还建议您直接使用输入列表,而不是将其复制到数组中。您可以复制列表并对其进行排序,或者根据需要对其进行操作。感谢您的帮助!如果我想将此方法设为int而不是boolean呢?而不是“return true”我应该写:“smallstright=20”而不是return counter==3。写“return smallstright?”你说的是得分吗?如果是,只要做If(hassallstright(dice)){score+=30;}谢谢。这有时是有效的。比如,当我在1,3,2,6,4滚动时,它会起作用。但是如果我滚动1,2,2,3,4,它就不起作用了。我想这与我之前遇到问题的重复项有关。除非我应该首先对数组进行排序?编辑:Nevermind,我进行了排序,但这也不起作用。只是尝试和{1,2,3,4}对我来说返回true,那么问题是什么呢?是的,你需要先对数组进行排序,然后再试一次,结果无效。让我尝试将我的代码放在顶部的编辑中。可能是我弄错了。这将完全删除重复项,而不是简单地将它们移到列表的末尾……啊,很抱歉,没有意识到OP只是想移动列表重复到最后..将修改我的答案我只是一个初级编程类,所以我们不能使用那样的for循环。有没有办法使用两个不同的for循环呢?编辑我的答案以使用简单的普通数组(因此有必要使用嵌套for循环)
    int[] a1 = { 1, 2, 2, 3, 4 };
    int[] a2 = new int[a1.length];
    int j = 0, k = 0;
    for (int i = 0; i < a1.length - 1; i++) {
        if (a1[i + 1] == a1[j]) {
            a2[k++] = a1[i + 1];
        } else {
            a1[++j] = a1[i + 1];
        }
    }
    System.arraycopy(a2, 0, a1, j + 1, k);
    System.out.println(Arrays.toString(a1));
[1, 2, 3, 4, 2]