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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA笔记- JAVA对象数组的遍历与使用详解

發布時間:2024/3/26 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA笔记- JAVA对象数组的遍历与使用详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1- 對象數組概述

  • 基本類型的數組:存儲的元素為基本類型
    int[] arr={1,2,3,4}
  • 對象數組:存儲的元素為引用類型
    Student[] stus=new Student[3];
    解釋::
    Student代表一個自定義類
    Stus數組中stus[0],stus[1],stus[2]的元素數據類型為Student,
    都可以指向一個Student對象
  • 2- 對象數組案例

    創建一個學生數組,存儲三個學生對象并遍歷

    /** 自動生成構造方法:* 代碼區域右鍵 -- Source -- Generate Constructors from Superclass... 無參構造方法* 代碼區域右鍵 -- Source -- Generate Constructor using Fields... 帶參構造方法* 自動生成getXxx()/setXxx():* 代碼區域右鍵 -- Source -- Generate Getters and Setters...*/ public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}package com.itheima; /** 創建一個學生數組,存儲三個學生對象并遍歷* * 分析:* A:定義學生類* B:創建學生數組* C:創建學生對象* D:把學生對象作為元素賦值給學生數組* E:遍歷學生數組*/ public class StudentDemo {public static void main(String[] args) {//創建學生數組Student[] students = new Student[3];//創建學生對象Student s1 = new Student("曹操",40);Student s2 = new Student("劉備",35);Student s3 = new Student("孫權",30);//把學生對象作為元素賦值給學生數組students[0] = s1;students[1] = s2;students[2] = s3;//遍歷學生數組for(int x=0; x<students.length; x++) {Student s = students[x];//System.out.println(s);System.out.println(s.getName()+"---"+s.getAge());}} }

    3- 對象數組的內存圖解

    總結

    以上是生活随笔為你收集整理的JAVA笔记- JAVA对象数组的遍历与使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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