1th Object Array

Source: Internet
Author: User

1.1 Overview of Object arrays
A: An array of basic types: the stored elements are basic types
Int[] arr={1,2,3,4}
B: Object array: The stored element is a reference type
Student[] Stus=new student[3];

Student represents a custom class
The element data type of stus[0],stus[1],stus[2] in the Stus array is student,
Can point to a student object
1.2 Object Array Case:
Create a student array, store three student objects and traverse
1.2.1 Case Code One:

  Package com.itheima;/* * automatically generates construction methods: * Code Area right--source--Generate constructors from superclass ...        Non-parametric construction method * Code Area right--source--Generate Constructor using fields ... Construction method with parameters * automatically generated getxxx ()/setxxx (): * Code Area right--source--Generate Getters and Setters ... */public class Student {private Stri ng 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.) {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());}}}

1.3 Memory diagram of an object array

1th Object Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.