Java基础00-集合基础15
2024-09-08 09:39:32
1. 集合基础
1.1 集合概述
1.2 ArrayList构造方法和添加方法
代码示例:
想通过指定索引添加到最后一个位置的话就填写,现有索引的+1,比如3个索引就是4.
如果在指定索引的位置添加没有的索引,就会报错(索引越界)。
运行结果:
1.3 ArrayList集合的常用方法
remove(Object o),删除指定的元素,返回删除是否成功。
如果是指定没有的元素,就是返回的false
remove(int index),删除指定索引|处的元素,返回被删除的元素。
运行结果:
指定删除未有的索引会报错(索引越界)
set(int index,E element)修改指定索引处的元素,返回被修改的元素。
运行结果:
指定修改未有的索引会报错(索引越界)
get(int index)返回指定索引处的元素。
运行结果:
int size()返回集合中的元素的个数。
运行结果:
1.4 案例
1.4.1 存储字符串并遍历
import java.util.ArrayList; public class ArrayList1 {
public static void main(String[] args) {
ArrayList<String> array= new ArrayList<>(); array.add("hellow");
array.add("world");
array.add("java"); for (int i=0; i<array.size(); i++){
String s = array.get(i);
System.out.println(s);
}
}
}
运行结果:
hellow
world
java
1.4.2 存储学生对象并遍历
public class Student {
private String name;
private int age; public Student(){} public Student(String name,int age){
this.name = name;
this.age = age;
} public void setName(String name){
this.name = name;
} public String getName(){
return name;
} public void setAge(int age){
this.age = age;
} public int getAge(){
return age;
}
}
import java.util.ArrayList; public class ArrayList2 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>(); //创建学生对象
Student s1 = new Student("tom",3);
Student s2 = new Student("Bob",4);
Student s3 = new Student("Amy",6); //添加学生对象到集合中
array.add(s1);
array.add(s2);
array.add(s3); //遍历集合,采用通用遍历格式实现
for (int i=0; i<array.size(); i++){
Student s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
} }
}
运行结果:
tom,3
Bob,4
Amy,6
1.4.3 存储学生对象并遍历
import java.util.ArrayList;
import java.util.Scanner; public class ArrayList4 {
public static void main(String[] args) {
ArrayList<Student1> array = new ArrayList<>(); //为增加代码复用性,采用方法来实现
addStudent1(array);
addStudent1(array);
addStudent1(array); for (int i=0; i<array.size(); i++){
Student1 s = array.get(i);
System.out.println(s.getName()+", "+s.getAge());
} } /*
明确:
返回值类型:void
参数:ArrayList<Student1> array
*/
public static void addStudent1(ArrayList<Student1> array){
Scanner sc = new Scanner(System.in); System.out.println("请输入学生名:");
String name = sc.nextLine(); System.out.println("请输入学生年龄:");
String age = sc.nextLine(); //创建学生对象
Student1 s = new Student1();
s.setName(name);
s.setAge(age); //在集合中添加学生对象
array.add(s); }
}
运行结果:
请输入学生名:
Tom
请输入学生年龄:
7
请输入学生名:
Bob
请输入学生年龄:
9
请输入学生名:
Amy
请输入学生年龄:
3
Tom, 7
Bob, 9
Amy, 3
最新文章
- web view调h5的方法死活调不到
- Linux通过NAT方式配置网络
- iptables Data filtering详解
- 门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统
- 【C++】第1章 在VS2015中用C++编写控制台应用程序
- angularjs 指令(directive)详解(2)
- linux修改登陆后进入的默认目录
- NDK jni 加载静态库
- nginx -t ";nginx: [warn] only the last index in ";index"; directive should be absolute in 6 ";的问题解决
- Toad for Oracle 使用文档
- java基础(十八)IO流(一)
- CentOS 6.7安装Tomcat 7
- java交通灯管理系统项目
- java-finalize
- mysql浅龟定
- IntelliJ IDEA 14.1.4设置关闭自动保存和标志改动文件为星号?
- Git中一些远程库操作的细节
- Winform下KeyDown,KeyPress,KeyUp事件的总结(转)
- C语句详细(初学者)
- idea+spring-boot+devtools热部署