Reputation:
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<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(45);
list.add(23);
list.add(48);
list.add(11);
System.out.println("Original list is " + list);
System.out.println("The smallest value is " + minimum(list)); // Expected: 11
modifyList(list, 5);
System.out.println("The new list is " + list);
}
public static void modifyList(List<Integer> list, int value)
{
for(int i = 0; i < value; i++);
list.get(i) += value;
}
Upvotes: 1
Views: 3022
Reputation: 154
So you have a couple of problems, first in the method modifyList()
the parameter List<Integer>
should actually be ArrayList<Integer>
. 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
Upvotes: 1
Reputation: 2318
I think you need to update your for
loop to go from 0 to the length of list
(for(int i = 0; i < list.size(); i++)
) instead of go from 1 to value
(for(int i = 0; i < 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 set
function for that (list.set(i, list.get(i) + value)
)
public static void modifyList(List<Integer> list, int value) {
for(int i = 0; i < 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<Integer> list, int n) {
for(int i = 0; i < n; i++)
list.add(list.remove(0));
}
Upvotes: 2
Reputation: 695
In your code there are 2 errors
Syntax for(int i = 0; i < 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.
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.
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.
When we do list.set(i, sum)
it overwrites the value at that index
with our 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);
}
}
Upvotes: 1
Reputation:
You should iterate over the 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);
}
}
Upvotes: 1