数组对象:(对象里面属性包含数组)本质是对象,即一个数组对象,该数组里面存储的数据类型可以是各种数据类型;
对象数组:(数组里面元素包含对象)对象数组是指数组元素是对象的数组:是由一组对象引用组成的数组;
测试案例:
Person 对象:
public class Person {
private String name;
private int age;
private String address;
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
super();
}
}
Car对象:
public class Car {
private String brand;
private String[] colour;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String[] getColour() {
return colour;
}
public void setColour(String[] colour) {
this.colour = colour;
}
public Car(String brand, String[] colour) {
super();
this.brand = brand;
this.colour = colour;
}
public Car() {
super();
}
}
测试Demo: (重点)文章来源:https://www.toymoban.com/news/detail-683111.html
import java.util.Iterator;
public class day18 {
public static void main(String[] args) {
// 数组对象:(对象里面属性包含数组)本质是对象,即一个数组对象,该数组里面存储的数据类型可以是各种数据类型;
Car car = new Car();
String[] s = {"黑色","白色","红色"};
car.setBrand("奥迪");
car.setColour(s);
String[] co = car.getColour();
System.out.println("数组对象:");
for (int i = 0; i < co.length; i++) {
System.out.println(car.getBrand()+"--"+co[i]);
}
System.out.println("---------------");
// 对象数组:(数组里面元素包含对象)对象数组是指数组元素是对象的数组:是由一组对象引用组成的数组;
Person[] p1 = new Person[3]; // 先初始化,开辟内存空间;
p1[0] = new Person("小婷",22,"宝鸡");
p1[1] = new Person("小霞",24,"西安");
p1[2] = new Person("小桐",26,"汉中");
System.out.println("对象数组:");
for (int i = 0; i < p1.length; i++) {
Person p = p1[i];
System.out.println(p.getName()+"--"+p.getAge()+"--"+p.getAddress());
}
}
}
测试输出:文章来源地址https://www.toymoban.com/news/detail-683111.html
数组对象:
奥迪--黑色
奥迪--白色
奥迪--红色
---------------
对象数组:
小婷--22--宝鸡
小霞--24--西安
小桐--26--汉中
到了这里,关于【Java 数组与对象】数组对象和对象数组的区别(127)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!