当前位置:网站首页>Set creation and common methods
Set creation and common methods
2022-07-25 10:15:00 【Look at the bugs】
The creation of sets and the use of methods
ArrayList<String> arr=new ArrayList<>();
// Output set
System.out.println(arr);
//public boolean add(E e); Appends the specified element to the end of this collection
arr.add("yan");
System.out.println(arr);
//public void add(int index ,E element), Add the element in the specified position
arr.add(1,"fu");
arr.add(2,"qiang");
System.out.println(arr);
//public boolean remove(object o) Deletes the specified element , Returns whether the deletion was successful
System.out.println(arr.remove("fu"));
System.out.println(arr);
//public E remove(int index) Delete the element where the index is made , Returns the deleted element
System.out.println(arr.remove(1));
System.out.println(arr);
//public E set(int index,E element) Modify the element at the specified index , Return the modified element
System.out.println(arr.set(0,"kan"));
System.out.println(arr);
//get(int index) Returns the element at the index
System.out.println(arr.get(0));
//size(); Returns the number of elements in the collection
System.out.println(arr.size());
// result
[]
[yan]
[yan, fu, qiang]
true
[yan, qiang]
qiang
[yan]
yan
[kan]
kan
1
Applications of collections
// The main function
// Calling method
public static void main(String[] args) {
addStudent(array);
addStudent(array);
addStudent(array);
arr(array);
}
// How to input students' information ( Method area )
public static void addStudent(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
// Enter student data
System.out.println(" Please enter the student's name ");
String name=sc.nextLine();
System.out.println(" Please enter the age of the student ");
int age=sc.nextInt();
// Create student objects Assign the data entered by the keyboard to the student object 1
Student s=new Student();
s.setName(name);
s.setAge(age);
// Add a student object to the collection
array.add(s);
}
// The method of traversing this set
public static void arr(ArrayList<Student> array){
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName()+s.getAge());
}
}
Creation of student class
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name=name;
this.age=age;
}
public Student() {
}
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;
}
Three ways to traverse a set
// iterator , Collection specific traversal methods ( You can use it )
Iterator<Student> it= list.iterator();
while (it.hasNext()){
Student s=it.next();
System.out.println(s.getName()+s.getAge());
}
// Ordinary for Traverse ( Use index )
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getName()+s.getAge());
}
// enhance for Ergodic set ( Commonly used )
for(Student s:list){
System.out.println(s.getName()+s.getAge());
}
边栏推荐
- 关闭brew执行命令时的自动更新
- PyTorch 对 Batch 中每个样本计算损失 Loss for each sample
- 贪吃蛇小游戏
- [machine translation] scones -- machine translation with multi tag tasks
- 拷贝过来老的项目变成web项目
- 代码整洁之道--直击痛点
- JSP details
- JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
- 安装 oh my zsh
- Filter filter details (listeners and their applications)
猜你喜欢
随机推荐
第五阶段第一周
Round to the nearest
记录一些JS工具函数
常用类的小知识
小程序调起微信支付
UE4 快速找到打包失败的原因
C3D模型pytorch源码逐句详析(二)
SSM integration (simple library management system to integrate SSM)
异常处理Exception
腾讯云之错误[100007] this env is not enable anonymous login
简易加法计算器
四舍五入取近似值
JSP details
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
Probability theory and mathematical statistics 3 discrete random variables and probability distributions (Part 2)
JDBC总结
升级 GLIBC 2.29 checking LD_LIBRARY_PATH variable... contains current directory error 解决方案
小程序分享功能
C3D模型pytorch源码逐句详析(三)
Detailed explanation of MySQL database









