为数组中的每个元素添加外部编号

时间:2014-02-15 19:33:46

标签: java arraylist jgrasp

我需要在ArrayList中为每个数字添加一个外部数字。我找了两天试图找到一个类似的答案但什么也没找到。

ArrayList<Integer> list = new ArrayList<Integer>();
  list.add(12);
  list.add(45);
  list.add(23);
  list.add(47);
  list.add(13);

public static void modifyList(ArrayList<Integer> theList, int value)
{
  for (int i = 0; i < theList.size(); i++)
  {
    theList.add(theList[i],value);
  }
}

我已尝试过ArrayList的添加功能的各种组合,但总会有不同的错误,我开始发疯了

4 个答案:

答案 0 :(得分:0)

您可以使用set方法。

Arraylist.set(index,value);

答案 1 :(得分:0)

从arraylist获取i值可以使用get方法。

你有:

theList.add(theList[i], value);

应该是:

theList.add(theList.get(i));

如果要更改列表中的i元素,则可以使用set方法。

theList.set(i, value);

答案 2 :(得分:0)

int a = theList.get(i) + your_addition;

theList.set(i, a);

答案 3 :(得分:0)

逻辑如下:

for (int i = 0; i < theList.size(); i++) {
            int oldValue = theList.get(i);
            int newValue = oldValue + value;
            theList.set(i, newValue);
        }