一,列表 List name = new ArrayList<>()
1.初始化列表:
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
1.list中添加,插入,删除元素;
添加方法是:list.add();
//将animallist中的元素添加到arlist
arlist.addAll(animallist);
插入方法是:list.add(1); 在指定位置插入:list.add(3,6)
删除方法是:list.remove();
2.列表的排序:
Collections.sort(list);#按照从小到大的方式
3.拼接两个列表:.all()比较常见
例如:
//默认尾部添加
animallist.add("dog");
animallist.add("cat");
animallist.add("fish");
//在指定位置添加
animallist.add(1,"fish");
animallist.add(3,"bird");
animallist.add(5,"cat");
ArrayList <String> arlist = new ArrayList <String>();
- ArrayList<Integer>l1=new ArrayList<Integer>();
- l1.add(1);
- l1.add(3);
- l1.add(5);
- ArrayList<Integer>l2=new ArrayList<Integer>();
- l2.add(2);
- l2.add(4);
- l2.add(6);
- ArrayList<Integer>merge=new ArrayList<Integer>();
- merge.addAll(l1);
- merge.addAll(l2);
4.列表的遍历
for循环通过列表的下标遍历
for (int index = 0; index < animallist.size(); index += 2)
{
System.out.print(animallist.get(index) + " ");
}
二,栈
1、栈的定义
栈(Stack):是只允许在一端进行插入或删除的线性表。首先栈是一种线性表,但限定这种线性表只能在某一端进行插入和删除操作。
栈顶(Top):线性表允许进行插入删除的那一端。
栈底(Bottom):固定的,不允许进行插入和删除的另一端。
空栈:不含任何元素的空表。
2,栈的常见操作:
Push() 元素入栈 ; pop() 栈顶元素出栈;peek() 访问栈顶元素
/*插入元素e为新的栈顶元素*/
Status Push(SqStack *S, ElemType e){
//满栈
if(S->top == MAXSIZE-1){
return ERROR;
}
S->top++;
S->data[S->top] = e;
return OK;
}
/*若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR*/
Status Pop(SqStack *S, ElemType *e){
if(S->top == -1){
return ERROR;
}
*e = S->data[S->top]; //将要删除的栈顶元素赋值给e
S->top--; //栈顶指针减一
return OK;
}
三,队列
1,队列的定义:队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表
2,常用操作:
- 入队 push:在队尾添加一个元素。
- 出队 poll:在队首移除一个元素。
- 判空 isEmpty:判断队列是否为空