<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 數組 > 原文: [https://javatutorial.net/java-array](https://javatutorial.net/java-array) 在本教程中,我將向您展示如何在 Java 中創建和使用數組 數組是同一類型的變量的集合。 數組有多種用途。 例如,您可能希望將所有價格存儲在一個數組中。 但是使數組真正有用的是可以使用存儲在數組中的值的方式。 在 Java 中聲明新數組的一般形式如下: ```java type arrayName[] = new type[numberOfElements]; ``` 其中`type`是基本類型(請在[教程](http://javatutorial.net/java-primitive-types "Java Primitive Types")中了解有關基本類型的更多信息)或對象。`numberOfElements`是將存儲到數組中的元素數。 此值不能更改。 Java 不支持動態數組。 如果需要靈活,動態的結構來保存對象,則可能需要使用某些 Java 集合。 有關更多詳細信息,請參見 [Java 集合教程](https://javatutorial.net/choose-the-right-java-collection)。 ## 總覽 Java 支持一維或多維數組。 ![java array](https://img.kancloud.cn/be/b7/beb7bd1846809c38952e466012d35aa2_600x312.jpg) 數組中的每個項目都稱為元素,并且每個元素都通過其數字索引進行訪問。 如上圖所示,編號從 0 開始。例如,第 4 個元素將在索引 3 處訪問。 ## 初始化數組 讓我們創建一個數組來存儲一個 5 人的小公司中所有雇員的薪水。 ```java int salaries[] = new int[5]; ``` 數組的類型(在本例中為`int`)適用于數組中的所有值。 您不能在一個數組中混合類型。 ## 將值放入數組 現在我們已經初始化了`salaries`數組,我們想要在其中添加一些值。 我們可以在初始化期間執行以下操作: ```java int salaries[] = {50000, 75340, 110500, 98270, 39400}; ``` 或稍后再執行以下操作: ```java int salaries[] = new int[5]; salaries[0] = 50000; salaries[1] = 75340; salaries[2] = 110500; salaries[3] = 98270; salaries[4] = 39400; ``` ## 遍歷數組 您可以像這樣調用特定元素的值來使用它: ```java System.out.println("The value of the 4th element in the array is " + salaries[3]); ``` 這將產生輸出: ```java The value of the 4th element in the array is 98270 ``` 也可以使用`for`循環或`while`循環遍歷數組中所有元素的值。 在我們先前的教程 [Java 循環](http://javatutorial.net/java-loops "Java Loops")中進一步了解循環 ```java public class ArrayExample { public static void main(String[] args) { int salaries[] = {50000, 75340, 110500, 98270, 39400}; for(int i=0; i<salaries.length; i++) { System.out.println("The element at index " + i + " has the value of " + salaries[i]); } } } ``` 上面程序產生的輸出是: ```java The element at index 0 has the value of 50000 The element at index 1 has the value of 75340 The element at index 2 has the value of 110500 The element at index 3 has the value of 98270 The element at index 4 has the value of 39400 ``` 注意`salaries.length`的使用。 Java 中的數組具有`length`屬性,該屬性返回數組的長度。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看