简体   繁体   English

如何将for循环迭代回到它在java中访问的最后一个索引

[英]How to iterate back a for loop to the last index it visited in java

I have an array of integer, and i am trying to iterate back the for loop to the last index it visited based on some condition , Lets say i have 2 9 15 19 23 28 37 elements in that loop i am giving a condition that if each element of that loop is greater than a number lets say 8 , it will process that element again. 我有一个整数数组,我试图根据一些条件迭代for循环到它访问的最后一个索引,让我说我有2 9 15 19 23 28 37元素在该循环中我给出的条件是,如果该循环的每个元素都大于一个数字,比方说8,它将再次处理该元素。

Here my code is 我的代码是

List<Integer> result = new ArrayList<Integer>();
int n = 6;
for (int i = 0; i < n; i++) {
    int h = 8;
    int r = a[i] - h;
    if (r <= 0) {
        result.add(1);
    } else if (r >= 0) {
        result.add(1);
    }
}

Here h is the array which holds the declared elements.r is an integer that checks if elements are greater than the hit element ie 8.So the condition is if the elements are less that h the arraylist will add 1, else the control will go back to the operation int r = a[i] -h, for the same element.For example , 2 is less that 8 the arraylist will add 1 , but for 9 the control will do that same minus operation and come to the else part and add 1 to the arraylist.The last element processed by the loop if not zero will not be added to the list.Is is possible? 这里的h是包含声明元素的数组.r是一个整数,用于检查元素是否大于命中元素,即8.如果条件是元素小于ar,则arraylist将添加1,否则控件将继续返回操作int r = a [i] -h,对于相同的元素。例如,2小于8,arraylist将添加1,但对于9,控件将执行相同的减号操作并进入else部分并向arraylist添加1.如果不是零,循环处理的最后一个元素将不会被添加到列表中。这是可能的吗? please help. 请帮忙。

You could be returned a step back by --i : 您可以通过--i退回一步:

if (a[i] - h > 0) {
    // a[i] is greater than h
    --i; // process a[i] again on the next iteration
}

As @Stefan Warminski noticed it will lead to an infinite loop because we always will process the first element than is greater than the h . 正如@Stefan Warminski注意到它将导致无限循环,因为我们总是会处理第一个元素而不是大于h

The workaround could be creating an array int[] changes the same length as the origin list and putting a value into an appropriate cell which will indicate how many times changes[i] we process an a[i] element: 解决方法可能是创建一个数组int[] changes与原始列表相同的长度并将值放入适当的单元格中,该单元格将指示我们处理a[i]元素的次数changes[i]

if (a[i] - h > 0 && changes[i]++ < N) { ... }

where N is how many times you want to process an element. 其中N是您想要处理元素的次数。

Complete code snippet: 完整的代码段:

int[] a = {2, 9, 15, 19, 23, 28, 37};
int[] changes = new int[a.length];

int h = 8;
int N = 2;

for (int i = 0; i < a.length; i++) {
    if (a[i] - h > 0 && changes[i]++ < N) {
        System.out.println(a[i] + " is processed " + changes[i] + " times");
        --i;
    }
}

Output: 输出:

9 is processed 1 times
9 is processed 2 times
15 is processed 1 times
15 is processed 2 times
19 is processed 1 times
19 is processed 2 times
23 is processed 1 times
23 is processed 2 times
28 is processed 1 times
28 is processed 2 times
37 is processed 1 times
37 is processed 2 times

Tip: declare the h variable outside the for statement, it won't change inside (there is no need to create a variable on every iteration). 提示:在for语句之外声明h变量,它不会在内部更改(不需要在每次迭代时创建变量)。

Note that the test r<=0 is useless : you can just take result.add(1) outside of the tests since you do that operation in any case. 请注意,测试r<=0是无用的:您可以在测试之外使用result.add(1) ,因为在任何情况下都执行该操作。

int h=8;
for(int i=0;i<n;i++){
    int r = a[i] -h;
    result.add(1);
    if(r >=0){
        'loop back'
    }
}

Then, if your goal is to "process that element again", do you really need to iterate back ? 然后,如果您的目标是“再次处理该元素”,您真的需要迭代回来吗? You already have the element, just process it again. 您已经拥有该元素,只需再次处理它。

List<Integer> result = new ArrayList<Integer>();
int n =6;
int h=8;
for(int i=0;i<n;i++){
    int r = a[i] -h;
    result.add(1); // Processed once
    if(r >=0){
        result.add(1); // Processed twice
    }
}
List<Integer> result = new ArrayList<Integer>();
int n =6;
for(int i=0;i<n;i++){
    int h=8;
    int r = a[i] -h;
    if(r <=0){
        result.add(1);
    }else if(r >=0){
        result.add(1); 
        i--;           
    }
}

Using this code you can process the number greater than 8 again.If r>=0 then i is decremented and the numbers greater than 8 is processed again. 使用此代码,您可以再次处理大于8的数字。如果r> = 0,则i递减,并且再次处理大于8的数字。

If I understand correctly, you want to process an element multiple times, only when that element meets some condition. 如果我理解正确,您希望多次处理一个元素, 只有当该元素满足某些条件时。

You can simply loop over the list and then if the element is greater than h , you just execute the desired action (in my case System.out.println() ) an n number of times. 你可以简单地遍历列表,然后如果元素是大于h ,只需执行所需的操作(在我的情况System.out.println()n的次数。

List<Integer> list = Arrays.asList(2, 9, 15, 19, 23, 28, 37);

int h = 8;
int n = 2;
list.stream().forEach(t -> {
    int r = (t > h ? n : 1);
    for (int i = 0; i < r; i++) {
        System.out.println(t);
    }
});

Or the below-Java-8 version: 或者以下Java-8版本:

List<Integer> list = Arrays.asList(2, 9, 15, 19, 23, 28, 37);

int h = 8;
int n = 2;
for (int t : list) {
    int r = (t > h ? n : 1); // Execute 'n' times if condition is met,
                             // otherwise, execute once
    for (int i = 0; i < r; i++) {
        System.out.println(t); // Code you want to execute
    }
}

Besides, 除了,

int h = 8;
int r = a[i] - h;
if (r <= 0) {
    result.add(1);
}
else if (r >= 0) {
    result.add(1);
}

doesn't make sense. 没有意义。 In fact, you are comparing a[i] with h . 事实上,你正在比较a[i]h The following code has the same effect: 以下代码具有相同的效果:

int h = 8;
result.add(1);
if (a[i] == h) {
    // Add the number 1 again to the list
    result.add(1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM