簡體   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?

我正在學習 java,正在學習 Java 中的 arrays。我想嘗試編寫一個程序來遍歷數組中的每個元素,並使用程序查看元素是增加、減少還是數組的一部分正在增加的方法並且數組的一部分正在減少。 我對如何執行此操作感到很困惑,並希望獲得有關我需要修復的內容的一些指導。 我猜我的嘗試可能與我想要發生的事情相去甚遠。

我不太確定如何遍歷數組並測試每個單獨的元素。 我試過了,但我做錯了。 我試圖制作一個 for 循環,它將遍歷數組的元素。 在 for 循環中,我嘗試制作一個 if 語句,我希望它通過數組中的每個元素到 go,然后檢查數組的順序是否為遞增順序,然后是 output“遞增”、遞減順序和 output“遞減” ”,或者如果數組的一部分在增加而一部分在減少,我希望 output 是“增加和減少”。 (例如:{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。這種情況會一直發生,直到該方法循環遍歷數組中的所有元素,它會 output增加。 如果數組中的每個元素都小於后續元素,則檢查順序是否從最小到最大,然后該方法將 output 遞增。 然后,如果數組僅反向減少,我會做同樣的事情。 我沒有正確執行此操作,請提供幫助。

這是我嘗試檢查數組是否從最小到最大的方法。 我認為它只是檢查了 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");
        }

    }
}

注意:如果您確定下跌后上漲不被視為“上漲和下跌”,則循環中的第一個條件將是:

if (decreasing == true)
    break;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM