JAVA数组
public class Main { public static void main(String[] args) { // char a[][]={{"ff"},{"ee"}}; char c[][] = { { 'a', 'c' }, { 'c', 'd' } }; String[][] list = { { "1", "张三" }, { "2", "李四" }, { "3", "王五" } }; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { System.out.println(c[i][j]); } } System.out.println(".............."); for (int i = 0; i < list.length; i++) { for (int j = 0; j < list[0].length; j++) { System.out.println(list[i][j]); } } } }
public class Main { public static void main(String[] args) { // char a[][]={{"ff"},{"ee"}}; char c[][] = new char[3][4]; String[] list = { "1230", "ewfr", "sddf" }; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { c[i][j] = list[i].charAt(j); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { System.out.println(c[i][j]); } } } }
Arrays.fill() 填充数组
public static void fill(int[] a, int fromIndex, int toIndex, int val)
Arrays类的fill() 方法是用来输入给定数组中元素值的。
1、两个参数
public static void fill(int[] a, int val):给定一个数组,一个val值
含义为为数组a进行赋值,使得其所有元素值都为val。
2、四个参数
public static void fill(int[] a, int fromIndex, int toIndex, int val):给定一个数组,起始位置fromIndex(包含),末尾位置toIndex(不包含),对范围内的元素进行赋值,示例如下:
int[] arr = new int[6]; Arrays.fill(arr, 60); //此时,arr的值为[60, 60, 60, 60, 60, 60] Arrays.fill(arr, 3, 5, 100); //此时,arr的值为[60, 60, 60,100, 100, 60]
binarySearch()方法
https://blog.csdn.net/qpzkobe/article/details/78897762
binarySearch(Object[], Object key)
a: 要搜索的数组
key:要搜索的值
如果key在数组中,则返回搜索值的索引;否则返回-1或“-”(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素的索引。
技巧:
[1] 搜索值不是数组元素,且在数组范围内,从1开始计数,得“ - 插入点索引值”;
[2] 搜索值是数组元素,从0开始计数,得搜索值的索引值;
[3] 搜索值不是数组元素,且大于数组内元素,索引值为 – (length + 1);
[4] 搜索值不是数组元素,且小于数组内元素,索引值为 – 1。
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Map<String, Integer> map = new HashMap<>(); int nums[] = { 1, 2, 4, 8 }; int index1 = Arrays.binarySearch(nums, 2); int index2 = Arrays.binarySearch(nums, 0); int index3 = Arrays.binarySearch(nums, 9); int index4 = Arrays.binarySearch(nums, 6); System.out.println("2: " + index1 + " 0: " + index2 + " 9: " + index3 + " 6: " + index4); } }
System.arrayCopy
https://blog.csdn.net/qq_32405269/article/details/78374035
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码解释:
Object src : 原数组
int srcPos : 从元数据的起始位置开始
Object dest : 目标数组
int destPos : 目标数组的开始起始位置
int length : 要copy的数组的长度
int[] a = new int[10]; a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; System.arraycopy(a, 2, a, 3, 3); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); }
0 1 2 2 3 0 0 0 0 0
Arrays.copyOfRange
Arrays.copyOf()
int[] arr1 = { 1, 2, 3, 4, 5 }; int[] arr2 = Arrays.copyOf(arr1, 4); int[] arr3 = Arrays.copyOf(arr1, 10); for (int i = 0; i < arr2.length; i++) System.out.print(arr2[i] + " "); System.out.println(); for (int i = 0; i < arr3.length; i++) System.out.print(arr3[i] + " "); }
1 2 3 4
1 2 3 4 5 0 0 0 0 0
List<Integer> list = new LinkedList<>(); for (int i = 2; i <= 5; i++) list.add(i); Integer a[] = list.toArray(new Integer[list.size()]); for (int x : a) System.out.println(x);
List<int[]> list = new LinkedList<>(); int aa[][] = { { 7, 0 }, { 4, 4 }, { 7, 1 }, { 5, 0 }, { 6, 1 }, { 5, 2 } }; for (int p[] : aa) { list.add(p); } int bb[][] = list.toArray(new int[list.size()][]); for (int x[] : bb) { System.out.println(x[0] + " " + x[1]); }
static public String[] qu(String[] a) { int cnt = 0; Set set = new HashSet<>(); for (String x : a) { set.add(x); } Object[] ob = set.toArray(); for (Object x : ob) { a[cnt++] = (String) x; } return Arrays.copyOfRange(a, 0, cnt); }
数组相等
Arrays.equals(a,b)
int a[] = { 1, 2, 5 }; // for (int x : a) { // x *= 2; // } // for (int y : a) { // System.out.println(y);// 1 2 5 // } for (int i = 0; i < a.length; i++) { a[i] *= 2; } for (int y : a) { System.out.println(y);// 2 4 10 }