Sorry for the vague title, hopefully I can make my question clearer here.
I have some two-dimensional List<List<Double>> myList
that contains two items each sublist: one explicit double
, such as 2.0
, and one double
variable. Essentially what I want to do is instead of storing some constant value in the list, I want it to store a reference to the variable, because the variable will be changing in other parts of the program and the list should reflect that. Here is a very simplified version of what I want to do:
import java.util.Arrays;
import java.util.List;
public class Example {
static double apples = 10.0;
static double oranges = 5.0;
static List<List<Double>> myList = Arrays.asList((Arrays.asList(2.0, apples)), (Arrays.asList(2.0, oranges)));
public static void main(String[] args) {
apples += 3;
oranges += 3;
for (List<Double> list : myList) {
// list.get(1) -= list.get(0);
System.out.println(list.get(1) - list.get(0));
}
}
}
The program output is the following:
8.0
3.0
However, it should be:
11.0
6.0
Because apples
and oranges
were both increased by 3. Furthermore, notice the commented out line list.get(1) -= list.get(0);
. What I ultimately want to do is reduce the variable specified by list.get(1)
by the amount specified by list.get(0)
. However, when I uncomment this line I get:
Error:(20, 25) java: unexpected type
required: variable
found: value
I am thinking that both of these issues are due to the value of apples
and oranges
being stored in the list as constant values, and not a reference to the actual variables themselves. Is this because of me using Arrays.asList()
to create myList
? Or is it a problem in the way I define List<List<Double>>
? Or is it some other problem altogether?
Thank you for any help.