当前位置:网站首页>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());
}
边栏推荐
猜你喜欢

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查

Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()

UE4源码的获取和编译

GCD详解

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

Use of dictionary tree

Download and installation of QT 6.2

PyTorch 代码模板 (CNN)

Common methods of nodejs version upgrade or switching

广度优先遍历(图和二叉树的层序遍历相关问题)
随机推荐
ROS distributed operation -- launch file starts nodes on multiple machines
VoxCeleb1 数据集下载
Reflection 反射
Debug篇快捷键入门
四舍五入取近似值
Ubuntu20.04系统下安装MySQL数据库5.7.29版本
oh-my-zsh和tmux配置(个人)
几个常用的网络诊断命令
拷贝过来老的项目变成web项目
关闭brew执行命令时的自动更新
21. Merge Two Sorted Lists
C3D模型pytorch源码逐句详析(一)
JDBC总结
Dynamic planning, shopping list problem
Redux使用和剖析
JDBC操作数据库详解
MVC三层架构理解
线程池的死锁事件
小程序H5获取手机号方案
TCP传输