最实用的Java数组算法
    Thursday, 3 August 2017
    12:30
    最实用的Java数组算法
    数组是程序设计语言中算法的基础。在Java中,容器的底层实现就是用的数组。今天和大家分享的技术干货是Java数组。以笔者工作多年的经验来看,无论是求职面试还是实际运用,你所遇到的问题脱不开以下内容。掌握了这些,就好比张无忌学会了九阳神功,剩下的就是对这些知识点的灵活运用了。
    JavaatH
    一张图罗列技术要点,知识系统不碎片化
    一维数组
    定义:用于存储同一类型数据的一个容器,可以对该容器中的数据从0开始编号,数组是对象。
    声明数组

    1. - 元素类型 [ ] 变量名 = new 元素类型 [元素的个数];
    2. - 元素类型 [ ] 变量名 = {元素1,元素2...};
    3. - 元素类型 [ ] 变量名 = new 元素类型[ ]{元素1,元素2...};

    数组元素个数:length
    下标运算:[ ]

    1. - 数组的元素是通过索引访问的。数组索引从 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]);
    数组实例1
    二维数组
    定义:二维数组可以看成以数组为元素的数组。
    int
    数组声明: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
    应用:表格、矩阵、棋盘、地图
    算法之数组拷贝
    public class ArrayTest2 {  public static void  String[] SSI = {"1", "2", "3"};  String[] ss2 = SSI;  ss2[Ø] =  System. ;
    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. ;
    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]
    public static  String[]  SSI  String[]  ss2  void test4(){  Arrays. copyOf(ss1, ssl.length);  System.out.print1n(Arrays. toString(ss2)); // [1,  2,  3]
    小编在上一篇文章《一张图搞定Java数组,工作面试掌握这些就够了(上)》中,分享了一维数组和二纬数组的相关知识,今天这篇文章是数组中的算法篇,我在这里分享几个很实用的数组算法,初级程序员工作面试掌握这些就够了,太多了也没必要。头条号“一张图学Java”分享的内容就是以实用为主,看了就会,会了就能用上,以最少的时间精力掌握最多的实用技术。
    声 明  元 忄 : 两  能 见 鲦 : A Inde 总 Ou 旧 fBOund 、 《 《 ion  俐 内 存  龙 “ 数 绢  自 “ ay , 工 具 类
    Java数组的知识导图
    数组拷贝
    public static void testl(){  String[]  SSI {"1",  String[]  SS2 SSI;  ss2[0]  System. out. ) ;
    方式一
    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
    方式二
    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]
    方式三
    public static  String[]  SSI  String[]  ss2  void test4(){  Arrays. copyOf(ss1, ssl.length);  System.out.print1n(Arrays. toString(ss2)); // [1,  2,  3]
    方式四
    数组扩容
    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]
    常见面试题: 统计字符的位置
    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;
    排序
    // java  public static void arraySort(int[] array) {  Arrays . sort(array) ;  System. out. print In (Arrays . toString( array) ) ;
    方式一
    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) ) ;
    方式二
    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) ) ;
    方式三
    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) ) ;
    方式四
    查找
    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;
    有序数组查找
    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;
    无序数组查找
    // Java  public static void test(int[]  array , int num){  int index Arrays. binarySearch(array, 8) ;  System. out. print In ( index) ;
    Java API 提供的查找方式

    1. - [Java](https://www.365yg.com/search/?keyword=Java)
    2. - /[编程语言](https://www.365yg.com/search/?keyword=%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80)
    3. - /[文章](https://www.365yg.com/search/?keyword=%E6%96%87%E7%AB%A0)
    4. - /[程序员](https://www.365yg.com/search/?keyword=%E7%A8%8B%E5%BA%8F%E5%91%98)

    收藏
    举报
    已使用 Microsoft OneNote 2016 创建。