在 Java 中的 arrayList 中增加整数的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27205268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:58:20  来源:igfitidea点击:

Best way to increment Integer in arrayList in Java

javaarraylistinteger

提问by WVrock

What is the cleanest way to increment an Integer in an ArrayList?

在 ArrayList 中增加 Integer 的最简洁方法是什么?

ArrayList<Integer> ints = new ArrayList<>();
ints.add(5);
ints.add(9);

What is the cleanest way to increment the last element?

增加最后一个元素的最干净的方法是什么?

ints.set(ints.size() - 1, ints.get(ints.size() - 1) + 1);seems pretty ugly to me.

ints.set(ints.size() - 1, ints.get(ints.size() - 1) + 1);对我来说似乎很丑。

采纳答案by ursa

Maybe you need to use another structure of data?

也许您需要使用另一种数据结构?

LinkedList<AtomicInteger> list = new LinkedList<AtomicInteger>();
ints.add(new AtomicInteger(5));
ints.add(new AtomicInteger(9));

list.getLast().incrementAndGet();

回答by Sotirios Delimanolis

You can't increment the value in place since Integerobjects are immutable. You'll have to get the previous value at a specific position in the ArrayList, increment the value, and use it to replace the old value in that same position.

由于Integer对象是不可变的,因此您不能就地增加值。您必须在 中的特定位置获取先前的值ArrayList,增加该值,并使用它来替换同一位置的旧值。

int index = 42; // whatever index
Integer value = ints.get(index); // get value
value = value + 1; // increment value
ints.set(index, value); // replace value

Alternatively, use a mutable integer type, like AtomicInteger(or write your own).

或者,使用可变整数类型,例如AtomicInteger(或编写您自己的)。

回答by evernoob

I have done something like this:

我做了这样的事情:

arraylistofint.set(index, arraylistofint.get(index) + 1);

here is an example from my code (modified names):

这是我的代码中的一个示例(修改后的名称):

ArrayList<Integer> numBsPerA = new ArrayList<> ();
...
int cntAs = 0;
int cntBs = 0;
for ( TypeA a : AnArrayListOfAs )
   {
      numBsPerA.add(0);
      for ( TypeB b : a.getAnArrayListOfBs() )
      {
         numBsPerA.set(cntAs, numBsPerA.get(cntAs) + 1);
         cntBs++;
      }
      System.out.println(a.toString()+ " has "
                        +numBsPerA.get(cntAs)+" TypeBs");
      cntAs++;
   }
   System.out.println("Total number of As: "+cntAs); 
   System.out.println("Total number of Bs: "+cntBs);
   // can now loop over numBsPerA to check how many Bs per A or whatever.

回答by KEERTHAN SHETTY

I can done by the code using java ...In that we can Increment each element of the list

我可以通过使用 java 的代码来完成......因为我们可以增加列表的每个元素

List<Integer> list= new ArrayList<Integer>();
  //int[] primitive = List1.toArray(list1);
  int count=0;
  int count1=0;
  int k=0;
  list.add(3);list.add(2);list.add(3);list.add(5);
  System.out.println("The given input is");
  for(int i:list){
      System.out.println(i);
  }
  Integer[] a = list.toArray(new Integer[list.size()]);
  for(int i = 0; i <a.length; i++)
    {
        if(i==a.length-1){
            a[i]++;
            count++;
        }else if(i==a.length-2){
            a[i]++;
            count++;
        }else if(i==a.length-3){
            a[i]++;
            count++;
        }else if(i==a.length-4){
            a[i]++;
            count++;
        }

    }
  System.out.println("After first operation");
  for(int i:a){
      System.out.println(i);
  }

  System.out.println("the first count is"+count);
  for(int i = 0; i <a.length; i++)
    {
        if(i==a.length-1){
            a[i]--;
            count1++;
        }else if(i==a.length-2){
            a[i]--;
            count1++;
        }else if(i==a.length-3){
            a[i]--;
            count1++;
        }else if(i==a.length-4){
            a[i]--;
            count1++;
        }

    }

   for(int i:a){
      System.out.println(i);
  }
   System.out.println("the second count is"+count1);
  //int Count2=count+count1;
   System.out.println("After second operation");



  System.out.println("--------------------------------------------");
  for(int j:a){
      System.out.println(j);
  }
 List<Integer> list1=Arrays.asList(a);

  System.out.println("After conversion of list");
  for(int i:list1){
      System.out.println(i);
  }

i made the count also...

我也算了……

This question asked in one of the interview also..

在其中一次采访中也问到了这个问题。