最实用的Java数组算法
Thursday, 3 August 2017
12:30
最实用的Java数组算法
数组是程序设计语言中算法的基础。在Java中,容器的底层实现就是用的数组。今天和大家分享的技术干货是Java数组。以笔者工作多年的经验来看,无论是求职面试还是实际运用,你所遇到的问题脱不开以下内容。掌握了这些,就好比张无忌学会了九阳神功,剩下的就是对这些知识点的灵活运用了。
一张图罗列技术要点,知识系统不碎片化
一维数组
定义:用于存储同一类型数据的一个容器,可以对该容器中的数据从0开始编号,数组是对象。
声明数组:
- 元素类型 [ ] 变量名 = new 元素类型 [元素的个数];- 元素类型 [ ] 变量名 = {元素1,元素2...};- 元素类型 [ ] 变量名 = new 元素类型[ ]{元素1,元素2...};
数组元素个数:length
下标运算:[ ]
- 数组的元素是通过索引访问的。数组索引从 0 开始,所以索引值从 0 到 length-1.
常见的异常:ArrayIndexOutOfBoundsException![public class Array Testel { public static void main(String[] args) { String[] name = { 't BEE", "*E!" , // int[] age -new int[] { 23, 24, 25 y; // float[] salaries —new float[name. length]; // System. out. print In (name . length) ; System. out. printin (Arrays . toString( name) ) ; salaries [ e ] = 3eeef; salaries [1] = seeef; salaries [2] = 45eef•, // iBfi%Å for (int i = e; i < salaries. length; i++) { name[i], age[i], salaries[i]);](/uploads/projects/wsbo@ekqhd3/3fceec0943e1c3849f0193c989025a48.jpeg)
数组实例1
二维数组
定义:二维数组可以看成以数组为元素的数组。

数组声明:Java中多维数组的声明和初始化应从高维到低维的顺序进行。
![int = new int[3][]; a[0] = new int[2]; all] = new int[4]; al 2] = new int[3]; // int [ ] ] b = new
下标运算:需要两次下标运算才能取出元素
![int [ ] [ ] System. out. [2]); //5](/uploads/projects/wsbo@ekqhd3/25b28c12828db07ff06ecc0708f92b36.jpeg)
应用:表格、矩阵、棋盘、地图
算法之数组拷贝![public class ArrayTest2 { public static void String[] SSI = {"1", "2", "3"}; String[] ss2 = SSI; ss2[Ø] = System. ;](/uploads/projects/wsbo@ekqhd3/bec6039d9f2a7fe8f28974f3239e0072.jpeg)
![public static void test2(){ String[] SSI — String[] ss2 = new String[ssl. length]; for (int i — O; i < ss2.1ength; i++) { ss2[i] — ssl[i]; ss2[Ø] - , System. out. ;](/uploads/projects/wsbo@ekqhd3/5077a937683f07305056a77296a8edb2.jpeg)
![public static void test3(){ String[] SSI String[] ss2 new String[ssl. length] ; System. arraycopy(ssl, 0, ss2, 0, ssl.length); System.out.print1n(Arrays. toString(ss2)); // [1, 2, 3]](/uploads/projects/wsbo@ekqhd3/f8c804d8b407e920563a0b97a082e6be.jpeg)
![public static String[] SSI String[] ss2 void test4(){ Arrays. copyOf(ss1, ssl.length); System.out.print1n(Arrays. toString(ss2)); // [1, 2, 3]](/uploads/projects/wsbo@ekqhd3/cc58a0c03508c68f955a6e1f826870c0.jpeg)
小编在上一篇文章《一张图搞定Java数组,工作面试掌握这些就够了(上)》中,分享了一维数组和二纬数组的相关知识,今天这篇文章是数组中的算法篇,我在这里分享几个很实用的数组算法,初级程序员工作面试掌握这些就够了,太多了也没必要。头条号“一张图学Java”分享的内容就是以实用为主,看了就会,会了就能用上,以最少的时间精力掌握最多的实用技术。
Java数组的知识导图
数组拷贝![public static void testl(){ String[] SSI {"1", String[] SS2 SSI; ss2[0] System. out. ) ;](/uploads/projects/wsbo@ekqhd3/c6638f32aa5ece02e5e2afd71d7477ee.jpeg)
方式一![public static void test2(){ String[] SSI String[] ss2 new String[ssl. length] ; for (int i = 0; i < ss2.1ength; i++) { ss2[i] ssl[i] ss2[0] System. out. print In(ssl ) ; //1](/uploads/projects/wsbo@ekqhd3/4b1d13304d61f2d9809c8adf05d94c53.jpeg)
方式二![public static void test3(){ String[] SSI String[] new String[ssl.length]; ss2 System.arraycopy(ssl, 0, ss2, 0, ssl.length); System.out.print1n(Arrays. toString(ss2)); // [1, 2, 3]](/uploads/projects/wsbo@ekqhd3/f938610d614ad8c10a2dbe068181b1e7.jpeg)
方式三![public static String[] SSI String[] ss2 void test4(){ Arrays. copyOf(ss1, ssl.length); System.out.print1n(Arrays. toString(ss2)); // [1, 2, 3]](/uploads/projects/wsbo@ekqhd3/c1508e978d18086d330540f3135e4b33.jpeg)
方式四
数组扩容![public static void testl(){ arryl — String[] arryl = Arrays. copyOf(arry1, arry1.1ength+2); System. out. println(arryl . length); //5 System.out.print1n(Arrays. toString(arry1)); EET , null, null]](/uploads/projects/wsbo@ekqhd3/b004a6c79e28878cfccd1f959111b73b.jpeg)
常见面试题: 统计字符的位置![public static void test2(){ String str = ; int[] arry = countALL2(str, ' // [4' 7] System. out. print1n(Arrays . tostring( arry) ) ; private static int[] countA112(String str„char ch){ int[] array I}; for (int i = O; i < str.length(); i++) { if (ch —z str.charAt(i)) { array Arrays . copyOf(array, array. length+l) ; array [array. length-I] return array;](/uploads/projects/wsbo@ekqhd3/60f3d7076fe3dece1baa4dc463ba9a3a.jpeg)
排序![// java public static void arraySort(int[] array) { Arrays . sort(array) ; System. out. print In (Arrays . toString( array) ) ;](/uploads/projects/wsbo@ekqhd3/53b9fce8a8c00db0adce8a978359bf77.jpeg)
方式一![public static void array) { int temp o; for (int i 0; i < array. length - 1; i++) { for (int j i + 1; j < array. length; j4+) { if (array[j] < array[i]) { array[l] temp array [i] array [j]; array[J] temp ; System. out. println (Arrays . toString( array) ) ;](/uploads/projects/wsbo@ekqhd3/2d281c06bf6947dd67e94414db417610.jpeg)
方式二![public static void selectSort(int[] array) { int temp = 0; for (inti 0; i < array. length - 1; i++) { int min for (int j i + 1; j < array. length; j4+) { if (array[min] > array[j]) { min if (min i) { temp = array[i]; array[i] array [min]; array [min] = temp; System. out. print1n(Arrays . toString( array) ) ;](/uploads/projects/wsbo@ekqhd3/cca6040ed3a26ae2b62183cdb12c156f.jpeg)
方式三![public static int temp = 0; for (int i for (int j void array) { < array. length; i4+) { if (array[j]<array[j-l]) { array [J] temp array[J] array [j -1]; array[j-l] temp; }else break; System. out. println (Arrays . toString( array) ) ;](/uploads/projects/wsbo@ekqhd3/ff53054061f1c1d3e0f04bf1b1256b69.jpeg)
方式四
查找![public static int array int low = e; int high = array. length-I; while(low = (Iow+high)/2; int mid if (num>array[mid]) { low — mid + 1; }else if (num<array[mid ] ) { high = mid - 1; }else { return mid; , int return -1;](/uploads/projects/wsbo@ekqhd3/f7a8e39ec8072fa68551de550ad8612d.jpeg)
有序数组查找![public static int search(int[] array , int num){ int index 1; for (int i = e; i < array. length; i++) { if (num==array[i]) { index — i; return index;](/uploads/projects/wsbo@ekqhd3/6d6f4182f459123ee016e8bfe8270f34.jpeg)
无序数组查找![// Java public static void test(int[] array , int num){ int index Arrays. binarySearch(array, 8) ; System. out. print In ( index) ;](/uploads/projects/wsbo@ekqhd3/7186655709bec6882f20cfe7177f9f0e.jpeg)
Java API 提供的查找方式
- [Java](https://www.365yg.com/search/?keyword=Java)- /[编程语言](https://www.365yg.com/search/?keyword=%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80)- /[文章](https://www.365yg.com/search/?keyword=%E6%96%87%E7%AB%A0)- /[程序员](https://www.365yg.com/search/?keyword=%E7%A8%8B%E5%BA%8F%E5%91%98)
收藏
举报
已使用 Microsoft OneNote 2016 创建。
