如何将数组列表中的每个元素逐个增加 n 个元素?

huangapple go评论73阅读模式
英文:

How do I increment every element in an array list by n elements?

问题

我只在此任务中编辑底部的方法我正在尝试通过n或"value"元素来增加列表的每个元素但我似乎无法找到方法这是我迄今为止的方法找到最小值的方法没有显示但其按预期工作我只想修改第二个方法的主体部分

public class ArrayListLab 
{
   
   public static void main(String[] args) 
   {          
      ArrayList<Integer> list = new ArrayList<Integer>();
      list.add(12);
      list.add(45);
      list.add(23);
      list.add(48);  
      list.add(11);      
      System.out.println("原始列表为:" + list);
      System.out.println("最小值为:" + minimum(list));   // 预期结果:11
      modifyList(list, 5);
      System.out.println("新列表为:" + list); 
   }
  

   public static void modifyList(List<Integer> list, int value)
   {
      for(int i = 0; i < list.size(); i++)
         list.set(i, list.get(i) + value);
}
英文:

I am only editing the bottom methods for this assignment. I am trying to increase every element of the list by n or "value" elements but I can't seem to figure out how. This is my approach so far. There is a method for finding the min not shown that works as intended. I am only trying to modify the body of the second method

public class ArrayListLab 
{
   
   public static void main(String[] args) 
   {          
      ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
      list.add(12);
      list.add(45);
      list.add(23);
      list.add(48);  
      list.add(11);      
      System.out.println(&quot;Original list is &quot; + list);
      System.out.println(&quot;The smallest value is &quot; + minimum(list));   // Expected: 11
      modifyList(list, 5);
      System.out.println(&quot;The new list is &quot; + list); 
   }
  

   public static void modifyList(List&lt;Integer&gt; list, int value)
   {
      for(int i = 0; i &lt; value; i++);  
         list.get(i) += value;

   }

答案1

得分: 2

我认为你需要更新你的for循环,使它从0循环到list的长度(for(int i = 0; i &lt; list.size(); i++)),而不是从1循环到value(for(int i = 0; i &lt; value; i++))。你还需要移除for语句末尾的;,这样Java程序将执行下面的列表更新代码,而不是循环执行一个空的代码块。

关于如何更新ArrayList对象,你可以使用set函数来实现(list.set(i, list.get(i) + value))

public static void modifyList(List<Integer> list, int value) {
    for(int i = 0; i &lt; list.size(); i++) 
        list.set(i, list.get(i) + value);
}

更新:要实现像你提到的旋转ArrayList的操作,你可以使用remove(0)函数来移除并返回列表中的第一个元素,然后使用add函数将它添加到列表的末尾,就像你在main函数中所做的一样。

public static void rotateList(List<Integer> list, int n) {
    for(int i = 0; i &lt; n; i++) 
        list.add(list.remove(0));
}
英文:

I think you need to update your for loop to go from 0 to the length of list (for(int i = 0; i &lt; list.size(); i++)) instead of go from 1 to value (for(int i = 0; i &lt; value; i++)). You also need to remove the ; at the end of for statement so the Java program will take the list update code below instead of looping through an empty block of code.

Regarding how to update an ArrayList object, you can use the setfunction for that (list.set(i, list.get(i) + value))

public static void modifyList(List&lt;Integer&gt; list, int value) {
    for(int i = 0; i &lt; list.size(); i++) 
        list.set(i, list.get(i) + value);

}

Update: To rotate the ArrayList like you mention, you can remove and return the first element in the list using the function remove(0) and the just add it to end of the list using the add function like you did in the main function

public static void rotateList(List&lt;Integer&gt; list, int n) {
    for(int i = 0; i &lt; n; i++) 
        list.add(list.remove(0));
}

答案2

得分: 1

你应该对列表进行迭代。

    public static void modifyList(List<Integer> list, int value)
           {
              for(int i = 0; i < list.size(); i++){  
                 list.set(i, list.get(i) + value);
              }
        
           }
英文:

You should iterate over the list.

    public static void modifyList(List&lt;Integer&gt; list, int value)
           {
              for(int i = 0; i &lt; list.size(); i++){  
                 list.set(i, list.get(i) + value);
              }
        
           }

答案3

得分: 1

所以你有一些问题,首先在方法modifyList()中,参数List&lt;Integer&gt;实际上应该是ArrayList&lt;Integer&gt;。此外,你的for loop在行末应该有{而不是

遍历ArrayList的一种更简单的方式是

for(Integer i : list) {list.set(i, i+value); }

另外,minimum(list)没有定义。

查看https://www.w3schools.com/java/java_arraylist.asp,获取有关arraylists的帮助。

英文:

So you have a couple of problems, first in the method modifyList() the parameter List&lt;Integer&gt; should actually be ArrayList&lt;Integer&gt;. Furthermore, your for loop must have a { instead of ; at the end of the line.

An easier way to loop through ArrayLists is

for(Integer i : list) {list.set(i, i+value); }

Also, the minimum(list) is undefined.

Check out https://www.w3schools.com/java/java_arraylist.asp for help on arraylists

答案4

得分: 1

在你的代码中有两个错误

  1. 逻辑错误
  2. 语法错误

语法错误:for(int i = 0; i &lt; value; i++); 中有一个;,这会导致你的变量 i 无法被识别,或者说它会超出范围。

逻辑错误:
当你使用 list.get(i) 时,它只会返回与该索引关联的值。你可以像这样做:int sum = list.get(i) + value,这会从列表中获取索引 i 处的 value,然后将数字 5 加到它上面。所以现在你的 sum 包含了你想要的 索引 i 处的值 + 5

现在你要做的就是将索引 i 处的值设置为我们计算得出的 sum。当我们执行 list.set(i, sum) 时,它会用我们的 sum 覆盖那个索引处的值。

public static void modifyList(ArrayList<Integer> list, int value) {
    for (int i = 0; i < value; i++) {
        int sum = list.get(i) + value;
        list.set(i, sum);
    }
}
英文:

In your code there are 2 errors

  1. Logical
  2. Syntax

Syntax for(int i = 0; i &lt; value; i++); there's a ; and cause of that your variable i wont get recognized or say it would be out of scope.

Logical:
When you did list.get(i) all it does is, Gives you the value associated with that index.<br>
You can do like int sum = list.get(i) + value what this will do it get the value from the list at index i and then add the number 5 to it.<br> So now your sum holds value from index i + 5 which you wanted.

Now all you gotta do it set the index at i to the sum we calculated.<br>
When we do list.set(i, sum) it overwrites the value at that index with our sum.

public static void modifyList(ArrayList&lt;Integer&gt; list, int value)
	   {
	      for(int i = 0 ; i &lt; value ;i++) {
           int sum = list.get(i) + value;
	    	list.set(i,sum);  
	      }
	   }

huangapple
  • 本文由 发表于 2020年9月27日 12:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64084660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定