迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 向数组中添加对象

作者:迹忆客 最近更新:2023/09/11 浏览次数:

本教程介绍如何在 Java 中将对象添加到自定义类的数组中。

Java 是一种面向对象的编程语言,一切都围绕着对象展开。所有数据结构容器(List、Array、Set、set)都以对象形式存储/保存数据。我们也可以创建一个自定义类的数组,并将对象存储到其中。

在本文中,我们首先创建一个自定义类的数组,然后将这个类的对象存储到 this 中。所以,让我们从一些例子开始。


在 Java 中将对象添加到自定义类的数组

在这个例子中,我们创建了一个包含三个字段的 Student 类,在 SimpleTesting 类中,我们创建了一个这个类的数组,稍后我们将一个对象存储到这个数组中。请参阅下面的示例。

public class SimpleTesting{
	public static void main(String[] args){
		Student[] studentArray = new Student[5];
		Student student = new Student(1, "Rohan", 40);
		studentArray[0] = student;		
		System.out.println(studentArray[0].getId());
		System.out.println(studentArray[0].getName());
		System.out.println(studentArray[0].getAge());
	}
}
class Student{
	int id;
	String name;
	int age;
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
}

输出:

1
Rohan
40

在 Java 中将对象添加到自定义类的列表中

这是存储自定义类对象的另一种解决方案。在这里,我们使用列表而不是数组来添加对象。与数组相比,列表有几个优点,例如内置方法支持和动态调整大小。

在这里,我们首先创建了一个仅接受学生类对象的列表,然后我们创建了一个学生类对象并使用 add() 方法将其添加到列表中。

访问列表元素。我们使用了 get() 方法。请参阅下面的示例。

import java.util.ArrayList;
import java.util.List;

public class SimpleTesting{
	public static void main(String[] args){

		List<Student> studentList = new ArrayList<>();
		Student student = new Student(1, "Rohan", 40);
		studentList.add(student);
		
		System.out.println(studentList.get(0).getId());
		System.out.println(studentList.get(0).getName());
		System.out.println(studentList.get(0).getAge());
	}
}

class Student{
	int id;
	String name;
	int age;
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
}

输出:

1
Rohan
40

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便