📄 newlistdemo.java
字号:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class NewListDemo
{
//创建新的List序列集合类型对象list,使其存储内容为String字符串类型数据
NewList<String> list=new NewList<String>();
public NewListDemo()
{
//向list对象中添加数据元素
list.add("wangxyw");
list.add("java");
list.add("IBM");
list.add("Employee");
//显示添加到list中的元素
System.out.println("The elements of list are:");
String str1[] = new String[4];
for (int i = 0; i < str1.length; i++)
{
str1[i] = list.get(i);
System.out.println(str1[i]);
}
//修改list对象中元素并显示
System.out.println("\nAfter modified ,the elements of list are:");
//修改指定位置上元素的值
list.set(0,"Wang Xiao Yue");
list.set(3,"student");
//显示list中数据
String str2[] = new String[4];
for (int i = 0; i < str2.length; i++)
{
str2[i] = list.get(i);
System.out.println(str2[i]);
}
//删除list对象中元素并显示
System.out.println("\nAfter removed ,the elements of list are:");
//删除list对象中,指定位置上的元素
list.remove(0);
//显示list中数据
String str3[] = new String[3];
for (int i = 0; i < str3.length; i++)
{
str3[i] = list.get(i);
System.out.println(str3[i]);
}
}
public static void main(String[] args)
{
new NewListDemo();
}
}
class NewList<Value>
{
//创建新的List序列集合类型对象list,使其存储内容为Value通配类型
public List<Value> list = new LinkedList<Value>();
//方法过载:向LinkedList中添加数据
public void add(Value v)
{
list.add(v);
}
//方法过载:获取LinkedList中index指定位置的数据
public Value get(int index)
{
return list.get(index);
}
//方法过载:获取Iterator对象
public Iterator<Value> iterator()
{
return list.iterator();
}
//方法过载:移除LinkedList中index指定位置的数据
public Value remove(int index)
{
return list.remove(index);
}
//方法过载:设置LinkedList中index指定位置的数据
public Value set(int index,Value v)
{
return list.set(index,v);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -