• Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
閱讀、跑步與書寫 ~~只要努力學習,知識一定能夠成為力量

JAVA學習筆記 ArrayList

2022 年 5 月 6 日
Home Reading Programming

        ArrayList類是一個可以動態修改的數組,簡單來說可以想像成一個任意調整大小(格數)的表格, 沒有固定大小的限制,可以添加或刪除元素。

import java.util.ArrayList; // 引入ArrayList 類
ArrayList<E> objectName =new ArrayList<>();  // 初始化
E是引用數據類型

基本操作方式

  1. 添加元素-add

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);
System.out.println(test);
}
}

執行結果:

[A, B, C, D]

批量增加,可以使用addAll,Collections.addAll(test,”A”,”B”,”C”,”D”);

  1. 訪問指定元素-get

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);
System.out.println(test.get(2));
}
}

 執行結果:

[ C ]

為什麼第2值不是B?因為索引值由由0開始算起。

  1. 修改元素-set

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);

        test.set(2, “CC”); //第一個參數為索引位置,第二個參數為修改的值。
System.out.println(test);
}
}

 執行結果:

[ A, B, CC, D]

  1. 刪除元素-remove

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);

        test.remove(2); //刪除第3個元素。
System.out.println(test);
}
}

 執行結果:

[ A, B, D]

刪除元素之後,原先第4個元素就會成為第3個,所以,如果再添加元素,元素會加在最後位置。

  5.計算元素量-size

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);
System.out.println(test.size());
}
}

 執行結果:

[ 4 ]

其它操作方式:

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);

        for (int i=0 ; i<test.size; i++){ // 0-3 ,執行4次
System.out.println(test.get(i));

        }
}
}

 執行結果:

[ A, B, C, D ]

import java.util.ArrayList;
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“A”);
test.add(“B”);
test.add(“C”);
test.add(“D”);

        for (String i:test) {   // for each
System.out.println(i);

        }
}
}

 執行結果:

[ A, B, C, D ]

Collection 排序

import java.util.ArrayList;

import java.util.Collections; //引入collection類
public class Practice {
public static void main(String[] args) {
ArrayList<String> test = new ArrayList<String>();
test.add(“D”);
test.add(“B”);
test.add(“C”);
test.add(“A”);

        test.add(“A”);

        Collections.sort(test);

        for (String i:test) {   // 字母排序
System.out.println(i);

        }
}
}

執行結果:

[ A, B, C, D ]

最大距陣練習,輸入20個值,顯示最大的值。

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Practice {

    public static void main(String[] args) throws Exception {

        int[] array = initializeArray();

        int max = max(array);

        System.out.println(max);

    }

分三部分進行,第一部分先建構這個練習的架構,宣告一個叫做array的陣列,放入一個叫做initializeArray()的method裡面。

第二部分宣告一個叫做max的int,將array的陣列放入一個叫做max的method裡面。

第三部分顯示max的結果。

    public static int[] initializeArray() throws IOException {

 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        int[] test = new int[20];

        for (int i =0 ; i<20; i++){

        test[i] = Integer.parseInt(reader.readLine()); 

        }

        return test;  

    }

        initializeArray()method,宣告一個做test的21格容量的int陣列,for loop20次讀取輸入值,返回陣列test,陣列array放入陣列test的值。

     public static int max(int[] array1) {

       int max = array1[0] ;

       for (int i=1; i<20 ; i++ ){

         if (array1[i]>max){

           max = array1[i];           

          }  

       }

       return max;

    }

}

        最後是max method的寫法,陣列array放入去,max裡面是讀取陣列int[]的值,取名為array1,宣告int max為array1[0]的值,如果後面的值大於max,就會取代成為max,for loop19次,返回max的值。

*method裡面的陣列因為是局部變數,所以可以全部取名為array,只是為了清楚理解取值所以才改了三個名字。

Tags: 程式學習JAVA
Share132Tweet83Share33
Charlie chacha

Charlie chacha

Related Posts

持續買進
Finance

關於投資,讀《持續買進》的心得

2024 年 4 月 24 日

今天我們來分享一下最近非常熱門的財經書籍《持續買進》,怎麼說呢?整本書讀下來,也是有所收獲,可是,我個人覺得沒有外界所說那麼高度評價。 其實這本書所說的策略或建議也不是什麼新鮮的東西,只是作者用了一個...

EXCEL VLOOKUP 文字轉數字 數字轉文字
Programming

自學Excel系列 – 使用Vlookup函數的1個小技巧, 文字與數字之間的轉換問題(文字轉數字 數字轉文字)

2023 年 7 月 6 日

Vlookup函數 Vlookup的泛用性相信學過Excel的人都應該知道,有時候因為資料的性質,我們無法(最主要是沒有時間)一格一格去改變資料的屬性,導致我們使用Vlookup時無法找到目標的值,所...

自學Excel VBA系列-如何用VBA控制OutLook?
Lifestyle

Excel強大的組合函數練習,資料庫關鍵字搜索,Address + Match + Index + Find 組合拳

2023 年 3 月 18 日

想要把Excel的功能變得強大,函數組合使用必不可少,例如之前介紹的Offset函數,《一個強大的Excel函數OFFSET,動態選取資料範圍》,非常好用。本文介紹另外一個經常會使用到的組合拳:Add...

AI人工智能
Market

探索人工智能,了解OpenAI的未來與潛力–ChatGPT使用心得 ChatGPT可以幫到你做什麼?人工智能 語言生成 數據分析

2023 年 7 月 6 日

OpenAI ChatGPT ChatGPT的名號,大家都應該非常熟悉。地區破解什麼就不討論,兩個條件,地區VPN,以及SMS認證服務,還不能是虛擬號碼,我花了幾天時間也找不到方便安全的省錢方法,建議...

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

About Me

閱讀、跑步與書寫 ~~只要努力學習,知識一定能夠成為力量

Charlie Chacha

Blogger and Runner

Welcome to my blog! I'm Charlie Chacha, and here I'll be sharing valuable insights on living a fulfilling life and achieving financial success. Join me as we delve into topics such as effective time management, personal growth, and the art of learning. I firmly believe that putting in the effort and embracing continuous learning is the key to unlocking a prosperous future. So, let's embark on this journey together and discover how to thrive in both life and finances!

Categories

  • Finance (37)
  • Lifestyle (7)
  • Market (22)
  • Programming (13)
  • Reading (41)
  • Research (21)
  • Running (41)
  • Sports (2)
  • Travel (3)
  • Uncategorized (1)

Popular

  • JAVA學習筆記 ArrayList

    一個強大的Excel函數OFFSET,動態選取資料範圍

    2401 shares
    Share 960 Tweet 600
  • Excel VBA的自學心得分享

    1915 shares
    Share 766 Tweet 479
  • 自學Excel VBA系列-如何用VBA控制WORD?

    1430 shares
    Share 572 Tweet 358
  • 自學Excel VBA系列-如何用VBA控制OutLook?

    1189 shares
    Share 476 Tweet 297
  • 自學Excel系列 – 使用Vlookup函數的1個小技巧, 文字與數字之間的轉換問題(文字轉數字 數字轉文字)

    621 shares
    Share 248 Tweet 155

Instagram

    Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to connect your Instagram account.
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me