LeetCode小点心

在我自己的杯中,饮了我的酒吧,朋友。               
一倒在别人的杯里,这酒的腾跳的泡沫便要消失了。          
Take my wine in my own cup, friend.
It loses its wreath of foam when poured into that of others.

https://blog.csdn.net/syysyf99/article/details/106211336

  1. Arraylist和数组互相转换

    网上搜Arraylist和数组互相转换的方法时,举的例子都是String类型的

​ 但是对于int类型如果这样写:

1
2
ArrayList<Integer> a=new ArrayList<Integer>();
int[] array=(int[])a.toArray(new int[size]);//会报错

则会报错,这是因为int[]并不等同于Integer[]。因此如果换成Integer[]数组,则能正确运行。

1
2
3
4
5
6
7
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
Integer[] array = list.toArray(new Integer[list.size()]);//能正确运行
for(int element:array){
System.out.println(element);
}

如果非得希望得到int[]的话,只能用循环赋值来得到了。

1
2
3
4
int[] d = new int[list.size()];
for(int i = 0;i<list.size();i++){
d[i] = list.get(i);
}

如果既不想用循环,又想要得到int[],那就只能在jdk8中使用IntStream了。

调用函数

public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引

查看评论