简体   繁体   English

当我在 for 循环内迭代数组元素时,如何迭代 Java 中的数组元素?

[英]How do I iterate over elements of an array in Java while I am inside of a for loop to iterate over the elements of an array?

I am learning java and am learning arrays in Java. I wanted to try and make a program that goes through each element in the array and using a method the program sees if the elements are either increasing, decreasing, or part of the array is increasing and part of the array is decreasing.我正在学习 java,正在学习 Java 中的 arrays。我想尝试编写一个程序来遍历数组中的每个元素,并使用程序查看元素是增加、减少还是数组的一部分正在增加的方法并且数组的一部分正在减少。 I am pretty confused on how to do this and would like some guidance on what I need to fix.我对如何执行此操作感到很困惑,并希望获得有关我需要修复的内容的一些指导。 I am guessing that my attempt is probably very far from what I wanted to have happen.我猜我的尝试可能与我想要发生的事情相去甚远。

I am not really sure how to loop through the array and test each individual element.我不太确定如何遍历数组并测试每个单独的元素。 I attempted it but I did it incorrectly.我试过了,但我做错了。 I tried to make a for loop which would iterate through the elements of the array.我试图制作一个 for 循环,它将遍历数组的元素。 Inside of the for loop I tried making an if statement and I wanted it to go through each element in the array and then check if the order of the array was in increasing order and then output "Increasing", decreasing order, and output "Decreasing", or if part of the array was increasing and part of it was decreasing I wanted the output to be "Increasing and decreasing".在 for 循环中,我尝试制作一个 if 语句,我希望它通过数组中的每个元素到 go,然后检查数组的顺序是否为递增顺序,然后是 output“递增”、递减顺序和 output“递减” ”,或者如果数组的一部分在增加而一部分在减少,我希望 output 是“增加和减少”。 (example: {1,3,5,7,4,2} ) 1,3,5,7 increase and 4,2 is decreasing. (例如:{1,3,5,7,4,2})1,3,5,7 增加,4,2 减少。 So I have one loop to iterate through each element in the array and then another loop inside the first loop checking if the elements are increasing or decreasing in the array and outputting Increase or Decrease.所以我有一个循环来遍历数组中的每个元素,然后在第一个循环中进行另一个循环,检查数组中的元素是增加还是减少,并输出增加或减少。


public class increasingOrDecreasing {
    public static void main(String[] args) {

        int[] array1 = { 3, 8, 13, 29, 52, 81 };

        int[] array2 = { 77, 66, 55, 33, 22, 11 };

        int[] array3 = { 20, 30, 60, 40, 15, 2 };

        int[] array4 = { 10, 30, 50, 50, 60, 80 };

        // call the method
        increaseOrDecrease(array1);
        increaseOrDecrease(array2);
        increaseOrDecrease(array3);
        increaseOrDecrease(array4);
    }

    // create the method

    public static int increaseOrDecrease(int[] myArray) {
        // make a loop to check if all the integers of the array are in either
        // increasing or decreasing order or both


        for (int i = 0; i < myArray.length; i++) {

            // if the array is increasing
// What I wanted is for it to start at array i = 0 and have [i] go up by 1 until it loops through the end // of the array. Then if each element is less than the subsequent element it would output "Increasing": I // did not do this correctly and would like some help. I guess all I did is check if myArray[i] is less 
// than myArray[i+1]

// if the array was {1, 3, 6, 8} then the method would go through each element and find that index [0] < 
// index [1] and index[1] < index [2] and index[2] < index [3] and output Increasing and return a value of // 1 because the method would find that all the numbers are ordered smallest to largest.


            if (myArray[i] < myArray[i++]) {


                System.out.println("Increasing");
                return 1;
            }

// this is incorrect and does not do what I was hoping it would.
            // if the array is decreasing
            else if (myArray[i] > myArray[i++]) {
                System.out.println("Decreasing");
                return 2;
            }


// this is also incorrect
            // if the array is increasing and decreasing
            else (myArray[i] < myArray[i++] && myArray[i] > myArray[i++]) {
                System.out.println("Both increasing and decreasing");
                return 3;
            }
        }
        return 0;

    }

}

This is what I wanted: for each element check and see if the element is less than the next element in the array.这就是我想要的:检查每个元素并查看该元素是否小于数组中的下一个元素。 For the first array {3,8,17,25,89,94} it would loop through the array.对于第一个数组 {3,8,17,25,89,94} 它将遍历数组。 Index 0 is 3 which is less than index 1. It goes to element 1 which is 8 and index 1 is less than index 2 which is 17. This would keep happening until the method looped through all the elements in the array and it would output Increasing.索引 0 是 3,小于索引 1。它转到元素 1,即 8,索引 1 小于索引 2,即 17。这种情况会一直发生,直到该方法循环遍历数组中的所有元素,它会 output增加。 If every element in the array is smaller than the subsequent elements checking if the order is smallest to largest then the method would output Increasing.如果数组中的每个元素都小于后续元素,则检查顺序是否从最小到最大,然后该方法将 output 递增。 Then I would do the same thing if the array is decreasing only in reverse.然后,如果数组仅反向减少,我会做同样的事情。 I did not do this correctly and would like some help, please.我没有正确执行此操作,请提供帮助。

Here is what I tried to do to check if the array is from smallest to largest.这是我尝试检查数组是否从最小到最大的方法。 I think it just checked if [i] < [i++] but did not go through the entire array.我认为它只是检查了 if [i] < [i++] 但没有通过整个数组检查 go 。

      for (int i = 0; i < myArray.length; i++) {

            // if the array is increasing
// What I wanted is for it to start at array i = 0 and have [i] go up by 1 until it loops through the end // of the array. Then if each element is less than the subsequent element it would output "Increasing": I // did not do this correctly and would like some help. I guess all I did is check if myArray[i] is less 
// than myArray[i+1]

// if the array was {1, 3, 6, 8} then the method would go through each element and find that index [0] < 
// index [1] and index[1] < index [2] and index[2] < index [3] and output Increasing and return a value of // 1 because the method would find that all the numbers are ordered smallest to largest.


            if (myArray[i] < myArray[i++]) {


                System.out.println("Increasing");
                return 1;
            }
public class Eli {
   
    public static void main(String[] args) {
        int myArray[] = {12,5,4,7,2};
        boolean increasing = false, decreasing = false;
        for (int i = 0; i < myArray.length-1; i++) {
            
            if (myArray[i] < myArray[i+1]) {
                increasing = true;
            }
            if (myArray[i] > myArray[i+1]) {
                decreasing = true;
            }

        }
        if (increasing == true && decreasing == true) {
            System.out.println("Increasing and decreasing");
        }
        else {
            if (increasing == true)
                System.out.println("Increasing");
            else System.out.println("Decreasing");
        }

    }
}

Note: If you make sure that a rise after a fall is not considered a "rise and fall", the first condition in the loop will be:注意:如果您确定下跌后上涨不被视为“上涨和下跌”,则循环中的第一个条件将是:

if (decreasing == true)
    break;

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

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