ArrayList 类相关例题

使用 ArrayList 类存储从控制台上录入的数据,直到输入 0 为止,然后用此类来实 现冒泡排序。代码如下

public class Arrylist实现冒泡排序 {
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<>();
		System.out.println("请录入数据:");
		while (true) {
			Scanner sc = new Scanner(System.in);
			int num = sc.nextInt();
			if (num == 0) {
				break;
			}
			arrayList.add(num);
		}
		System.out.println(arrayList);
		for (int i = 0; i < arrayList.size() - 1; i++) {// 冒泡排序的实现 比较次数为arrayList.size() - 1
			for (int j = 0; j < arrayList.size() - i - 1; j++) {
				Integer tmp;// 定义中间变量
				if (arrayList.get(j).compareTo(arrayList.get(j + 1)) > 0) {// 比较两数的大小 大于0则说明前边的值大。这里可以根据大于0小于0来判断是升序还是降序
					tmp = arrayList.get(j + 1);// 中间变量
					arrayList.set((j + 1), arrayList.get(j));// set方法 使用后边的值替换前边的值
					arrayList.set(j, tmp);
				}
			}
		}
		System.out.println(arrayList);
	}
}

效果如下
在这里插入图片描述

定义学生类(Student)保存学生信息,学生信息包括学号(id)、姓名(name)、年龄 (age),使用 ArrayList 来保存学生信息,并迭代输出。


/**
 * 
 * @author Swpu 功能描述: 定义学生类(Student)保存学生信息,学生信息包括学号(id)、姓名(name)、年龄 (age),使用
 *         ArrayList 来保存学生信息,并迭代输出。
 */
public class ArrayList集合的应用 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("\t\t请录入学生信息,当录入学号为0时结束");
		int id;
		ArrayList<Student> arrayList = new ArrayList<>();
		while (true) {
			Student stu = new Student();
			System.out.print("学号:");
			id = sc.nextInt();
			if (id == 0) {//判断何时跳出循环
				break;
			}
			stu.setId(id);
			System.out.print("姓名:");
			stu.setName(sc.next());
			System.out.print("年龄:");
			stu.setAge(sc.nextInt());
			arrayList.add(stu);
//			System.out.println(stu);

		}
		System.out.println("学生的信息如下");
		System.out.println("学号\t姓名\t年龄");
		for (int i = 0; i < arrayList.size(); i++) {//输出ArrayList的信息
			System.out.println(
					arrayList.get(i).getId() + "\t" + arrayList.get(i).getName() + "\t" + arrayList.get(i).getAge());
		}
//		arrayList.forEach(System.out :: println);另一种输出ArrayList的方法
	}
}

class Student {
	private Integer id;
	private String name;
	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

运行效果如下:
在这里插入图片描述

假设我们要设计一个动物园的程序,其中有多种动物,每种动物都有自己的特性和行为。这时候,我们可以使用面向对象的设计思想来实现这个程序。 首先,我们可以定义一个抽象 Animal,它包含动物的基本属性和行为,例如: ```java public abstract class Animal { protected String name; protected int age; public Animal(String name, int age) { this.name = name; this.age = age; } public abstract void eat(); public abstract void sleep(); public abstract void makeSound(); } ``` 在这个抽象中,我们定义了动物的基本属性 name 和 age,以及三个抽象方法 eat、sleep 和 makeSound,表示动物的基本行为。由于每种动物的具体实现不同,因此这些方法只是声明而不实现,留给子去具体实现。 接下来,我们可以定义几个具体的动物子,例如: ```java public class Lion extends Animal { public Lion(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Lion is eating meat."); } @Override public void sleep() { System.out.println("Lion is sleeping."); } @Override public void makeSound() { System.out.println("Roar!"); } } public class Elephant extends Animal { public Elephant(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Elephant is eating grass."); } @Override public void sleep() { System.out.println("Elephant is sleeping."); } @Override public void makeSound() { System.out.println("Trumpet!"); } } ``` 在这些子中,我们重写了父中的抽象方法,并实现了具体的行为。例如,Lion 中的 eat 方法表示狮子吃肉,而 Elephant 中的 eat 方法表示大象吃草。 最后,我们可以在主程序中创建一个动物园对象,添加各种不同的动物,例如: ```java public class Zoo { private List<Animal> animals; public Zoo() { animals = new ArrayList<>(); } public void addAnimal(Animal animal) { animals.add(animal); } public void showAnimals() { for (Animal animal : animals) { System.out.println(animal.name + " is " + animal.age + " years old."); animal.eat(); animal.sleep(); animal.makeSound(); System.out.println(); } } } public class Main { public static void main(String[] args) { Zoo zoo = new Zoo(); zoo.addAnimal(new Lion("Simba", 3)); zoo.addAnimal(new Elephant("Dumbo", 5)); zoo.showAnimals(); } } ``` 在主程序中,我们创建了一个动物园对象 zoo,添加了一只狮子和一头大象,然后展示了它们的基本信息和行为。运行程序,可以看到如下输出: ``` Simba is 3 years old. Lion is eating meat. Lion is sleeping. Roar! Dumbo is 5 years old. Elephant is eating grass. Elephant is sleeping. Trumpet! ``` 通过这个例子,我们可以看到抽象和接口的使用。Animal 作为抽象,定义了动物的基本属性和行为,并声明了一些抽象方法,留给子去实现。而 Lion 和 Elephant 作为具体的子,继承了 Animal ,并实现了它的抽象方法,以便具体实现狮子和大象的行为。最后,在 Zoo 中,我们使用 Animal 的对象来表示动物,从而可以添加不同种的动物并展示它们的基本信息和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值