数组
-
定义
int arr[]; int[] arr2;
-
初始化
int arr[] = new int[]{1, 3, 5, 7, 9}; int[] arr2 = {2, 4, 6, 8, 10};
-
数组长度length
int[] array = new int[5]; System.out.println(array.length);
-
遍历
- 使用for循环
- 使用foreach
for (int x: arr2) { System.out.print(x + "\t"); // 2 4 6 8 10 }
-
填充fill
- 全部填充
void fill(int[] a, int val)
- 指定索引的元素填充
void fill(int[] a, int fromIndex, int toIndex, int val)
Arrays.fill(arr3, 2); for (int x: arr3) { System.out.print(x + "\t"); // 2 2 2 2 2 全部填充为2 } System.out.println(); Arrays.fill(arr3, 2, 3, 8); for (int x: arr3) { System.out.print(x + "\t"); // 2 2 8 8 2 填充指定索引 }
- 全部填充
-
排序sort(升序)
- 全部排序
void sort(int[] a)
- 指定索引排序
void sort(int[] a, int fromIndex, int toIndex)
int[] arr4 = {3, 7, 2, 1, 9}; Arrays.sort(arr4); for (int x: arr4) { System.out.print(x + "\t"); // 1 2 3 7 9 } System.out.println(); int[] arr5 = {3, 7, 2, 1, 9}; Arrays.sort(arr5, 1, 3); for (int x: arr5) { System.out.print(x + "\t"); // 3 2 7 1 9 }
- 全部排序
-
复制数组copy
- 复制数组并指定新数组长度
int[] copyOf(int[] original, int newLength)
- 指定索引范围赋值
int[] copyOfRange(int[] original, int from, int to)
int[] arr6 = {1, 2, 3, 4, 5}; int[] arr7 = Arrays.copyOf(arr6, 5); // 1 2 3 4 5 int[] arr8 = Arrays.copyOfRange(arr6, 1, 3); // 2 3 for (int x: arr7) { System.out.print(x + "\t"); } System.out.println(); for (int x: arr8) { System.out.print(x + "\t"); }
- 复制数组并指定新数组长度
-
比较数组equals,比较两者元素值是否相等
public static boolean equals(long[] a, long[] a2) 如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。 同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。
-
二分查找数组元素binarySearch
public static int binarySearch(Object[] a, Object key) 用二分查找算法在给定数组中搜索给定值的对象(Byte,Int,double等)。 数组在调用前必须排序好的。 如果查找值包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。
字符串
-
字符串长度length
String s3 = new String("Hello Java"); System.out.println(s3.length()); // 10
-
获取字符串索引indexof
str.indexOf(substr)//从头开始查找 str.lastIndexOf(substr)//从尾开始查找 String s4 = new String("how are you"); System.out.println(s4.indexOf("o")); // 1 从头开始查找 System.out.println(s4.lastIndexOf("o")); // 9 从尾开始查找
-
获取指定索引的字符charAt
String s5 = new String("Hello Java"); System.out.println(s5.charAt(4)); // o //java没有C++一样的用[x]输出字符串元素 !!!
-
字符串连接 +
String s = new String("Hello"); String s2 = new String("World"); System.out.println(s + " " + s2); // Hello World
-
字符串判等
String se = new String("Summer is so Hot"); String se1 = new String("Summer is so Hot"); String se2 = new String("summer is so hot"); String se3 = se; System.out.println(se == se1); // false 比较内存而非字符串内容 System.out.println(se == se3); // true System.out.println(se.equals(se1)); // true 比较字符串内容 System.out.println(se.equals(se2)); // false System.out.println(se.equalsIgnoreCase(se2)); // true 忽略大小写 System.out.println(se2.startsWith("summer")); // true 字符串开始 System.out.println(se2.endsWith("cold")); // false 字符串结尾
-
大小写转换
String sc = new String("hello WORLD"); String scl = sc.toLowerCase(); // hello world 转换为小写 String scu = sc.toUpperCase(); // HELLO WORLD 转换为大写 System.out.println(scl + " " + scu);
-
截取字串subString
String str;
(1)str=str.substring(int beginIndex);
截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
(2)str=str.substring(int beginIndex,int endIndex);
截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;
- 判断字符串中是否包含另一个字串
boolean contains(str):
特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。
所以,也可以用于对指定判断是否包含。
if(str.indexOf("aa")!=-1)
而且该方法即可以判断,也可以获取出现的位置
- 切割split
String[] split(regex);//按regex进行切割
String[] word=ss.split('\\s');
- 替换 replace
String replace(oldchar,newchar);
String replaceall(oldchar,newchar);//参数也可以为string
后续补充