【问题标题】:Is there a way to define the value of the index in an arraylist?有没有办法在数组列表中定义索引的值?
【发布时间】:2015-04-09 15:03:32
【问题描述】:

我今天有一个作业要交(已经很晚了,所以我不会因此而获得功劳,但问题正在困扰我),我无法弄清楚我的一生。分配如下: 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 发送到行尾吗?

【问题讨论】:

  • 不应该是list.remove(i) 而不是1 吗?
  • 别担心,我仍然偶尔会在我的代码中混淆 i 和 1。下次尝试了解输出,这将帮助您更快地调试应用程序。顺便说一句,最好在 for 循环声明中直接初始化变量 i。

标签: java arraylist integer


【解决方案1】:

我认为你并不完全理解 Java 语法。我这样说并不是为了自鸣得意,但代码可能不会像您认为的那样做。检查您的 Java 语法知识,然后重试。 :-)

【讨论】:

  • 这应该是一条评论。
  • @OldCurmudgeon 虽然我同意,但他没有足够的代表发表评论。
  • @OldCurmudgeon 抱歉,这里是新人 :-) 以后会知道的。
  • @JiříKantor - 实际上是我的错误 - 我应该检查你的积分值。
【解决方案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)));
    }

}

打印

[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;
}

【讨论】:

    【解决方案3】:

    你可以这样做

    int nbOccur = Collections.frequency(yourList, 10);
    yourList.removeAll(Collections.singleton(10));
    yourList.addAll(Collections.nCopies(nbOccur, 0));
    
    • 获取 10 的出现次数(n)。
    • 全部删除。
    • 添加一个包含 n 乘以 0 的 List。

    使用 Java 8 的单行将是

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

    【讨论】:

      【解决方案4】:

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

      伪代码

      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
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          看看我的回答

          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;
          }
          

          【讨论】:

            猜你喜欢
            • 2020-08-15
            • 2020-12-27
            • 2014-05-01
            • 2023-02-15
            • 1970-01-01
            • 2015-10-06
            • 1970-01-01
            • 1970-01-01
            • 2022-08-19
            相关资源
            最近更新 更多