conchincradle / leetcode

0 stars 0 forks source link

Java-List #2

Open conchincradle opened 2 years ago

conchincradle commented 2 years ago

很容易搞混,比如poll(), size()

ArrayList

Java.util.ArrayList类

add 方法

list.add(3); list.add(2,4); // add 4 into the index of 2, that is the third one

List list = Arrays.asList(1,2,3); System.println(list); it's final, cannot add new element

Integer[] nums = {1,2,3,4} - must Integer instead of int List list = Arrays.asList(nums);

List list = new ArrayList<>(); list.addAll(index, Arrays.asList(nums));

List list = new ArrayList<>(); boolean b = Collections.addAll(list,10,200,100); - single element

List list = new ArrayList<>(Arrays.asList(nums));

conchincradle commented 2 years ago

list.clear(); list.clone(); list.contains(element);

list.get(index) list.indexOf(element) - if not , return -1

list.isEmpty()

list.lastIndexOf() - if not, return -1

remove

list.remove(index) list.remove(element);

list.removeRange(start, end) - end not included

list.size()

int[] arrays = list.toArray();

conchincradle commented 2 years ago
public static void dfs(List<Integer> list){
    list.remove(0);
  }
    Integer[] nums = {1,2,3,4};
    List<Integer> list = new ArrayList<>();
    list.addAll(Arrays.asList(nums));
    System.out.println(list);
    dfs(list);
    System.out.println(list);
[1, 2, 3, 4]
[2, 3, 4]

java方法是传递的那个地址,比如list地址传递过去,改动的话就会改动原来的,相当于引用传递 如果是原生数据类型,比如 i = 0, 这样就算i = 1也只是这个变量改变了指向的地址而已,因为是原生的,地址固定,也变动不了

conchincradle commented 2 years ago

list of list

List<List> list = new ArrayList<List>(); standard annotation

list.add(new ArrayList(Arrays.asList(1,2,3)));

empty list

return new Collections.emptyList();

// 耗时情况对比 // 1. 4626ms,看源码实现,相当于new ArrayList(10) return new ArrayList(); // 2. 2297ms // return new ArrayList(0); // 3. 14ms // return new Collections.emptyList();

conchincradle commented 2 years ago

LinkedList

链表肯定和线性表不同的,所以不要记混了! Deque, stack, queue 都可以用linkedlist实现的

conchincradle commented 2 years ago

new ArrayList(Arrays.asList(nums[k],nums[i],nums[j])) 不能中间写(Arrays.asList...)