当前位置:网站首页>Parse the commonly used methods in the List interface that are overridden by subclasses
Parse the commonly used methods in the List interface that are overridden by subclasses
2022-08-02 22:32:00 【SSS4362】
解析ListThe commonly used in the interface is implemented subclasses override method
1 在index位置插入element元素
1.1 使用方法
List接口的实现子类.add(int index,Object element);
//返回值类型为void,The original set ofindexLocation and its elements will be moving back after a
1.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
Iterator iterator = arr.iterator();
while (iterator.hasNext()) {
Object next =iterator.next();
System.out.println(next);
}
System.out.println("在下标为1的位置插入字符串55后");
arr.add(1,"55");
//使用for循环进行遍历
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
}
}
}
1.3 示例代码运行截图
2 从indexPosition began to set all the elements of inside added
2.1 使用方法
CollectionThe realization of the collection of child.addAll(int index,Collection c);
//返回值类型为布尔类型,As long as add successful elements,就返回true
//indexLocation and its later elements need to move backc集合的size()
2.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
Iterator iterator = arr.iterator();
System.out.println("添加之前");
while (iterator.hasNext()) {
Object next =iterator.next();
System.out.println(next);
}
ArrayList arrayList=new ArrayList();
arrayList.add("88");
arrayList.add("22");
System.out.println("添加之后");
arr.addAll(1,arrayList);
//使用for循环进行遍历
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
}
}
}
2.3 示例代码运行截图
3 获取指定index位置的元素
3.1 使用方法
Collection接口的实现子类.get(int index);
//返回值为Object对象,Return you find object,If there is no subscript will throw an exception
3.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
System.out.println("indexThe subscript position exists");
System.out.println(arr.get(0));
System.out.println("indexThe subscript position does not exist");
//Throws the subscript cross-border exception the runtime exception,在控制台打印错误信息,程序中断,At the back of the statement is not to
System.out.println(arr.get(arr.size()));
System.out.println("后面的语句");
}
}
3.3 示例代码运行截图
4 返回objThe position of the object in the collection for the first time in
4.1 使用方法
ListThe realization of the interface under the subclass.indexOf(Object obj);
//返回值类型为int类型,与String类中的indexOfThe analogy memory
//If found no placeObject对象时,会返回-1,Found it returns the corresponding index
4.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
System.out.println("存在Object对象时");
System.out.println(arr.indexOf("12"));
System.out.println("不存在Object对象时");
System.out.println(arr.indexOf("88"));
}
}
4.3 示例代码运行截图
5 返回objObjects in the position of the first occurrence in the current set(从后往前)
5.1 使用方法
List接口的实现子类.lastIndexOf(Object o);
//返回值为int类型,从后往前找,If for the first time found in the collection andoThe content of the content of the object as,That is returned in the collection the subscript,If the traverse through hasn't found,就返回-1
5.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
System.out.println("存在Object对象时");
System.out.println(arr.lastIndexOf("12"));
System.out.println("不存在Object对象时");
System.out.println(arr.lastIndexOf("88"));
}
}
5.3 示例代码运行截图
6 移除指定index位置的元素
6.1 使用方法
List接口的实现子类.remove(int index);
//返回值为Object对象(Returns to the delete the location element),Because we want to know the subscript to delete what things
6.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
System.out.println("删除前");
for (Object temp01:
arr) {
System.out.println(temp01);
}
//因为String类重写了toString方法,所以可以直接打印
System.out.println("Delete the elements as:"+arr.remove(1));
System.out.println("删除后");
for (Object temp02:
arr) {
System.out.println(temp02);
}
}
}
6.3 示例代码运行截图
7 设置指定index位置的元素为element(替换)
7.1 使用方法
List接口的实现子类.set(int index,Object element);
//返回值为Object对象,Back to be replaced element object,We want to know who is be replaced
7.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("12");
arr.add("66");
arr.add("77");
System.out.println("替换前");
for (Object temp01:
arr) {
System.out.println(temp01);
}
//因为String类重写了toString方法,所以可以直接打印
System.out.println("The contents of a element is replaced as:"+arr.set(1,"99"));
System.out.println("替换后");
for (Object temp02:
arr) {
System.out.println(temp02);
}
}
}
7.3 示例代码运行截图
8 返回从fromIndex位置到toIndexBefore a the location set
8.1 使用方法
List接口的实现子类.subList(int index,Object element);
//返回值为List结合,Get a is a collection of,The collection will not change,类似于String类里面的subString方法
//如果下标不在范围内,会出现下标越界异常
8.2 示例代码
package Work;
import java.util.ArrayList;
import java.util.List;
public class Test07 {
public static void main(String[] args) {
List arr1 = new ArrayList();
arr1.add("12");
arr1.add("66");
arr1.add("77");
List arr2=arr1.subList(0,2);
System.out.println("Intercept the child after collection content is as follows:");
for (int i = 0; i <arr2.size();i++) {
System.out.println(arr2.get(i));
}
}
}
8.3 示例代码运行截图
边栏推荐
猜你喜欢
随机推荐
解析List接口中的常用的被实现子类重写的方法
es DELETE index 源码分析
J9数字论:互联网跨链桥有什么作用呢?
我靠这套笔记自学,拿下字节50万offer....
golang刷leetcode 数学(1) 丑数系列
thinkphp框架5.0.23安全更新问题-漏洞修复-/thinkphp/library/think/App.php具体怎么改以及为什么要这么改
Introduction of uncommon interfaces of openlayers
十六进制文本的字节序问题
光源控制器接口定义说明
「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解
【学习日记】win64配置openni的vs2022编译环境
golang刷leetcode 动态规划(13) 最长公共子序列
元旦快乐(2022)
7.25 - 每日一题 - 408
Geoserver+mysql+openlayers2
【C语言刷题】Leetcode169——多数元素
Mysql安装流程 【压缩版】
spack install reports an error /tmp/ccBDQNaB.s: Assembler message:
golang刷leetcode动态规划(12)最小路径和
Dynamically generate different types of orders, how do I deposit to mongo database?