[英]How to access variable outside with two looping in java
I have variable that access in the looping . 我有循环访问的变量。 But there is two looping for. 但是有两个循环。
Here is my code : 这是我的代码:
int x = 0;
int xx = 0;
for (int d1 = 0; d1 < d - 1; d1++) {
for (int d2 = d1 + 1; d2 < d; d2++) {
//--other code---
x = listt.indexOf(max);
xx = list2.indexOf(max2);
}
}
When I access the variable like this , I got value who the last. 当我这样访问变量时,我就知道最后一个谁。
int x = 0;
int xx = 0;
for (int d1 = 0; d1 < d - 1; d1++) {
for (int d2 = d1 + 1; d2 < d; d2++) {
//--other code---
x = list.indexOf(max);
xx = list2.indexOf(max2);
}
}
System.out.println(" " + x);
System.out.println(" " + xx);
The value who taken is the last value . 取值者是最后一个值。 How can I access the variable outside all loop to get all value ? 如何在all循环之外访问变量以获取所有值? Is there anyway to get all value ? 反正有获取所有价值的机会吗? I have tried with getter and setter method but value still like that. 我已经尝试使用getter和setter方法,但价值仍然像那样。
Thanks before . 之前谢谢。 . 。
If you wish to record every change in the x
and xx
variables, you should store them in some data structure. 如果希望记录x
和xx
变量中的每个更改,则应将它们存储在某些数据结构中。
For example, in an ArrayList : 例如,在ArrayList中:
int x = 0;
int xx = 0;
List<Integer> xHistory = new ArrayList<>();
List<Integer> xxHistory = new ArrayList<>();
for (int d1 = 0; d1 < d - 1; d1++) {
for (int d2 = d1 + 1; d2 < d; d2++) {
//--other code---
x = list.indexOf(max);
xx = list2.indexOf(max2);
xHistory.add(x);
xxHistory.add(x);
}
}
Now you can iterate over the two lists to recover all the values x
and xx
had during the loop, or simply print the whole lists : 现在,您可以遍历两个列表以恢复循环中所有x
和xx
的值,或者仅打印整个列表:
System.out.println(xHistory);
System.out.println(xxHistory);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.