JAVA笔记- JAVA对象数组的遍历与使用详解
生活随笔
收集整理的這篇文章主要介紹了
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对象数组的遍历与使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MZ628光学指纹识别模块
- 下一篇: UBUNTU16 64位编译VLC-2.