【问题标题】: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?当我在 for 循环中迭代数组的元素时,如何在 Java 中迭代数组的元素?
【发布时间】:2022-11-22 00:38:52
【问题描述】:

我正在学习 Java 并且正在学习 Java 中的数组。我想尝试编写一个程序来遍历数组中的每个元素,并使用程序查看元素是增加、减少还是部分数组增加而部分数组减少的方法。我对如何执行此操作感到很困惑,并希望获得有关我需要修复的内容的一些指导。我猜我的尝试可能与我想要发生的事情相去甚远。

我不太确定如何遍历数组并测试每个单独的元素。我试过了,但我做错了。我试图制作一个 for 循环,它将遍历数组的元素。在 for 循环中,我尝试制作一个 if 语句,我希望它遍历数组中的每个元素,然后检查数组的顺序是否为递增顺序,然后输出“递增”、递减顺序,并输出“递减” ”,或者如果数组的一部分在增加而一部分在减少,我希望输出是“增加和减少”。 (例如:{1,3,5,7,4,2})1,3,5,7 增加,4,2 减少。所以我有一个循环来遍历数组中的每个元素,然后在第一个循环中进行另一个循环,检查数组中的元素是增加还是减少,并输出增加或减少。


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;

    }

}

这就是我想要的:检查每个元素并查看该元素是否小于数组中的下一个元素。对于第一个数组 {3,8,17,25,89,94} 它将遍历数组。索引 0 为 3,小于索引 1。它转到元素 1,即 8,索引 1 小于索引 2,即 17。这种情况会一直发生,直到该方法遍历数组中的所有元素并输出增加。如果数组中的每个元素都小于后续元素,则检查顺序是否从小到大,然后该方法将输出递增。然后,如果数组仅反向减少,我会做同样的事情。我没有正确执行此操作,请提供帮助。

这是我尝试检查数组是否从最小到最大的方法。我认为它只是检查了 [i] < [i++] 但没有遍历整个数组。

      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;
            }

【问题讨论】:

  • 您的所有 myArray[i] &lt; myArray[i++] 表达式中的意思是 ++ 吗?每次评估时都会递增 i。我怀疑你想要myArray[i] &lt; myArray[i+1]
  • 注意:不需要有内循环。您可以通过成对比较元素(将每个元素与其相邻元素进行比较)来判断它是在增加、减少还是同时增加。
  • 你能不能给个样品。一些示例输入和预期输出...

标签: java arrays loops if-statement


【解决方案1】:

公开课伊莱{

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");
    }

}

} 注意:如果您确定下跌后上涨不被视为“涨跌”: 循环中的第一个条件是: 如果(减少==真) 休息;

【讨论】:

    猜你喜欢
    • 2015-07-25
    • 2020-02-26
    • 2013-11-21
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多