日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习笔记_匿名/ArrayList

發布時間:2023/12/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习笔记_匿名/ArrayList 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引用類型的一般使用步驟:

  • 導包
    import 包路徑.類名稱
    如果需要使用的目標類,和當前類位于同一個包下,則可以省略導包語句不寫
    只有java.lang包下的內容不需要導包,其它的包都需要import語句

  • 創建
    類名稱 對象名 = new 類名稱();

  • 使用
    對象名.成員方法名()

  • 獲取鍵盤輸入的一個int數字:int num = sc.nextInt()’
    獲取鍵盤輸入的一個字符串:String str = sc.next();

    public class Demo{public static void main(String[] args){//System.in代表從鍵盤進行輸入Scanner sc = new Scanner(System.in);int num = sc.nextInt();System.out.println(“輸入的int數字是:” + num);String str = sc.next();System.out.println(“輸入的字符串是:” + str);} }

    匿名對象只能使用唯一的一次,下次再用不得不再創建一個新對象
    如果確定有一個對象只需要使用唯一的一次,就可以使用匿名對象

    public class Demo{public static void main(String[] args){//左邊的one就是對象的名字Person one = new Person();one.name = “zhangsan”;//我叫zhangsanone.showName();//匿名對象new Person().name = “lisi”;new Person().showName();//我叫lisi} }

    匿名對象作為方法的參數

    public class Demo{public static void main(String[] args){//普通使用方法Scanner sc = new Scanner(System.in);int num = sc.nextInt();//匿名對象的方式int num = new Scanner(System.in).nextInt();System.out.println(“輸入的是:” + num);//使用一般寫法傳入參數Scanner sc = new Scanner(System.in);methodParam(sc);//使用匿名參數來進行傳參methodParam(new Scanner(System.in));}public static void methodParam(Scanner sc){int num = sc.nextInt();System.out.println(“輸入的是:” + num);}public static Scanner methodReturn(){//Scanner sc = new Scanner(System.in);//retrun sc;return new Scanner(System.in);} }

    Random類用來生成隨機數字,使用起來也是三個步驟

  • 導包
    import java.util.Random;

  • 創建
    Random r = new Random();

  • 使用
    獲取一個隨機的int數字(范圍是int所有范圍,正負都可)
    int num = r.nextInt()

  • public class Demo{public static void main(String[] args){Random r = new Random();int num = r.nextInt();System.out.println(“隨機數是:” + num);} }

    Random生成指定范圍的隨機數

    public class Demo{public static void main(String[] args){Random r = new Random();for (int i = 0;i < 100; i++){int num = r.nextInt(bound: 10);System.out.println(num);}} }

    對象數組

    ArrayList集合概述和基本使用
    數組的長度不可以發生改變
    但是ArrayList集合的長度是可以隨意變化的

    對于ArrayList來說,有一個尖括號代表泛型
    泛型:也就是裝在集合中的所有元素,全部都是統一的什么類型
    注意:泛型只能是引用類型,不能是基本類型

    注意事項:
    對于ArrayList集合來說,直接打印得到的不是地址值,而是內容
    如果內容為空,得到的是空的中括號

    public class Demo{public static void main(String[] args){//創建了一個ArrayList集合,集合的名稱是List,里面裝的全部都是String字符串類型的數據//JDK1.7+,右側的尖括號內可以不寫內容,但是<>本身還要寫ArrayList<String> list = new ArrayList<>();System.out.println(list);//向集合中添加一些數據,需要用到add方法list.add(“zhangsan”);System.out.println(list);}list.add(“lisi”);list.add(“wangwu”);System.out.println(list);list.add(100)//錯誤寫法,因為創建的時候尖括號泛型已經說是字符串 }

    ArrayList當中的常用方法

    public boolean add(E e): 向集合當中添加元素,參數的類型和泛型一致。返回值代表添加是否成功 備注:對于ArrayList集合來說,add添加動作一定是成功的,所以返回值可用可不用 但是對于其他集合來說,add添加動作不一定成功public E get(int index) 從集合當中獲取元素,參數是索引編號,返回值是對應位置的元素public E remove(int index) 從集合當中刪除元素,參數是索引編號,返回值就是被刪掉的元素public int size() 獲取集合的尺寸長度,返回值是集合中包含的元素個數

    如果希望向集合ArrayList當中存儲基本類型數據,必須使用基本類型對應的“包裝類”

    基本類型 包裝類(引用類型,包裝類都位于java.lang包下)
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

    從 JDK 1.5+開始,支持自動裝箱、自動拆箱
    自動裝箱:基本類型 —>包裝類型
    自動拆箱:包裝類型 —>基本類型

    public class Demo{public static void main(String[] args){ArrayList<String> listA = new ArrayList<>();//ArrayList<int> listB = new ArrayList<>();//錯誤寫法,泛型只能是引用類型,不能是基本類型ArrayList<Interger> listC = new ArrayList<>();listC.add(100);listC.add(200);System.out.println(listC);} }

    數值添加到集合
    生成6個1~13之間的隨機整數,添加到集合,并遍歷集合

    public class Test{public static void main(String[] args){//創建Random對象Random random = new Random();//創建ArrayList對象ArrayList<Interger> list = new ArrayList<>();//添加隨機數到集合for(int i = 0;i < 6;i++){int r = random.nextInt(33) + 1;list.add(r);}//遍歷集合輸出for(int i = 0;i < list.size(); i++){System.out.println(list.get(i));}} }

    對象添加到集合
    自定義4個學生對象,添加到集合,并遍歷

    public class Test{public static void main(String[] args){//創建集合對象ArrayList<Student> list = new ArrayList<Student>();//創建學生對象Student s1 = new Student(“zhangsan”,18);Student s2 = new Student(“zhangsan”,19);Student s3 = new Student(“zhangsan”,20);Student s4 = new Student(“zhangsan”,21);//把學生對象作為元素添加到集合中list.add(s1);list.add(s2);list.add(s3);list.add(s4);//遍歷集合for(int x = 0; x < list.size(); x++){Student s = list.get(x);System.out.println(s.getName()+”——“+s.getAge());}} }

    總結

    以上是生活随笔為你收集整理的Java学习笔记_匿名/ArrayList的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。