有没有办法在arraylist中定义索引的值?

时间:2015-04-09 15:03:33

标签: java arraylist integer

我今天有一份作业(已经很晚了,所以我不会因为这个问题而得到了解,但问题就是在吃我)我无法理解我的生活。赋值如下:withoutTen:返回已删除所有10个的给定数组的版本。                 其余元素应根据需要向左移动到数组的开头,                 并且数组末尾的空格应为0。                 所以{1,10,10,2}产生{1,2,0,0}。                 您可以修改并返回给定的数组或创建一个新数组。

            withoutTen({1, 10, 10, 2}) --> {1, 2, 0, 0}
            withoutTen({10, 2, 10}) --> {2, 0, 0}
            withoutTen({1, 99, 10}) --> {1, 99, 0}

我尝试了各种方法来使程序正常运行但失败了。

`

// arraylist已经定义为名称为list

的Integer类
    int i= 0;
    //loop
    for(i = 0; i < list.size(); i++)
    {
        if( list.get(i) == 10)
        {
             list.remove(i);
             list.add(0);
        }
     }
     return list;

` 这显示了正确的结果{1,2,0,0},但这是唯一的结果。任何人都可以向我解释,如果有一种方法可以改变索引的值,如果它等于10并将其作为0发送到该行的后面吗?

6 个答案:

答案 0 :(得分:2)

基本上 - 您将其视为一个复制过程,您可以在数组中fromto向前走,确保from在看到它时10跳过public int[] withoutTen(int[] a) { // Where we are copying to. int to = 0; // Where we are copying from. int from = 0; // Zero padding at the end so carry on 'till to gets there. while (to < a.length) { // Skip all 10s. while (from < a.length && a[from] == 10) { // Skip it. from += 1; } // Copy it (or 0 if we're past the end). a[to++] = from < a.length ? a[from++] : 0; } return a; } public void test() { int[][] tests = new int[][]{ {1, 10, 10, 2}, {10, 2, 10}, {1, 99, 10} }; for (int[] a : tests) { System.out.println("\t" + Arrays.toString(a) + " -> " + Arrays.toString(withoutTen(a))); } } 。 / p>

[1, 10, 10, 2] -> [1, 2, 0, 0]
[10, 2, 10] -> [2, 0, 0]
[1, 99, 10] -> [1, 99, 0]

打印

for

使用public int[] withoutTen(int[] a) { // Zero padding at the end so carry on 'till to gets there. for (int to = 0, from = 0; to < a.length; to++, from++) { // Skip all 10s. while (from < a.length && a[from] == 10) { // Skip it. from += 1; } // Copy it (or 0 if we're past the end). a[to] = from < a.length ? a[from] : 0; } return a; } 循环的等价物有点整洁:

{{1}}

答案 1 :(得分:2)

我认为你并不完全理解Java语法。我不是说要自以为是,但代码可能不会做你认为它做的。查看Java语法知识,然后重试。 : - )

答案 2 :(得分:1)

你可以简单地做

int nbOccur = Collections.frequency(yourList, 10);
yourList.removeAll(Collections.singleton(10));
yourList.addAll(Collections.nCopies(nbOccur, 0));
  • 获得10的出现次数(n)。
  • 全部删除。
  • 添加包含n次0的列表。

使用Java 8的单行程序

yourList.stream()
        .filter(i -> i != 10)
        .collect(Collectors.toList())
        .addAll(Collections.nCopies(Collections.frequency(yourList, 10), 0));

答案 3 :(得分:0)

您是否需要担心数据空间的复杂性?如果是的话 - 显然你可以做得更好 - 但这应该适合你的目的..

psudo代码

List<Integer> beforeList = Arrays.asList(1,10,10,0,2);

//create array
int[] myFinalArray = new int[beforeList.size]()
int arrayIdx =0;
for (Integer i : beforeList){
   if (i != 10){
         myFinalArray[arrayIdx++ ] = i;
   }
}
//print/return your finalArray

答案 4 :(得分:0)

谢谢大家的帮助,似乎我的代码几乎是正确的,但我确实让我的list.get()方法错了。我放了一个(1)而不是(i)这实际上给我带来了很多痛苦和错误。 for(int i=0; i<list.size();i++) { if(list.get(i)==10) { list.remove(i); list.add(0); }

答案 5 :(得分:0)

看看我对此的答案

public int[] withoutTen(int[] nums) {

   if (nums == null) {
    return nums;
  }

  int non10pos = 0;

  for (int i = 0; i < nums.length; ++i) {

    if (nums[i] != 10) {
      int temp = nums[i];
      nums[i] = nums[non10pos];  // move the non-zero number to position i
      nums[non10pos] = temp;  // move the non-10 number toward the front
      ++non10pos;
    }
    else{
      nums[i] = 0;
    }


  }

  return nums;
}
相关问题