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

最新文章

  1. web view调h5的方法死活调不到
  2. Linux通过NAT方式配置网络
  3. iptables Data filtering详解
  4. 门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统
  5. 【C++】第1章 在VS2015中用C++编写控制台应用程序
  6. angularjs 指令(directive)详解(2)
  7. linux修改登陆后进入的默认目录
  8. NDK jni 加载静态库
  9. nginx -t &quot;nginx: [warn] only the last index in &quot;index&quot; directive should be absolute in 6 &quot;的问题解决
  10. Toad for Oracle 使用文档
  11. java基础(十八)IO流(一)
  12. CentOS 6.7安装Tomcat 7
  13. java交通灯管理系统项目
  14. java-finalize
  15. mysql浅龟定
  16. IntelliJ IDEA 14.1.4设置关闭自动保存和标志改动文件为星号?
  17. Git中一些远程库操作的细节
  18. Winform下KeyDown,KeyPress,KeyUp事件的总结(转)
  19. C语句详细(初学者)
  20. idea+spring-boot+devtools热部署

热门文章

  1. docker安装nextcloud私人网盘,开启https配置证书
  2. Java on Visual Studio Code的更新 – 2021年5月
  3. PTA4题学习总结
  4. v-for和v-if不能同时使用
  5. SpringBoot实现通用的接口参数校验
  6. 「10.12」木板(数学)&#183;打扫卫生(神仙DP)
  7. 卢卡斯定理&amp;&amp;中国剩余定理
  8. 【复习】Listening and Reading Comprehension
  9. Redis热点key优化
  10. 10 一键部署LNMP网站平台