Java基础之二维数组

一、数组 创建数组

数组 创建数组

package com.chenheng.array.a;

/**
 * @author:
 * @create: 2021-04-18 19:44
 * @description:
 * 练习-数组最小值
 **/

import cn.hutool.core.util.ArrayUtil;
import com.chenheng.array.utils.MyArrayUtil;

/**
 * 首先创建一个长度是5的数组
 * 然后给数组的每一位赋予随机整数
 * 通过for循环,遍历数组,找出最小的一个值出来
 * 0-100的 随机整数的获取办法有多种,下面是参考办法之一:
 * (int) (Math.random() * 100)
 * Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可
 */
public class MinValueArray {

  public static void main(String[] args) {
    int[] a = new int[5];
    a[0] = (int)(Math.random() * 100);
    a[1] = (int)(Math.random() * 100);
    a[2] = (int)(Math.random() * 100);
    a[3] = (int)(Math.random() * 100);
    a[4] = (int)(Math.random() * 100);
    method02(a);
  }

  /**
   * 使用 cn.hutool.core.util.PrimitiveArrayUtil#min(int...)
   * @param a
   */
  public static void method02(int[] a) {
    System.out.println("数组中的各个随机数是:");
    MyArrayUtil.toString(a);
    System.out.println(ArrayUtil.min(a));
  }
  /**
   * 获取数组最小值
   * @param a 数组
   */
  public static void method01(int[] a) {
    System.out.println("数组中的各个随机数是:");
    MyArrayUtil.toString(a);
    System.out.println("本次练习的目的是,找出最小的一个值:");
    int min = a[0];
    for (int i = 0; i < a.length; i++) {
      if (min > a[i]) {
        min = a[i];
      }
    }
    System.out.println(min);
  }
}

二、数组 初始化数组

数组 初始化数组

package com.chenheng.array.b;

/**
 * @author:
 * @create: 2021-04-19 09:43
 * @description: 练习-数组反转
 **/

import cn.hutool.core.util.ArrayUtil;
import com.chenheng.array.utils.MyArrayUtil;
import org.apache.commons.lang3.ArrayUtils;

/**
 * 首先创建一个长度是5的数组,并填充随机数。 使用for循环或者while循环,对这个数组实现反转效果
 */
public class ReverseArray {

  private static int[] a;

  /**
   * 静态代码块的特点:随着类的加载而执行,而且只执行一次
   */
  static {
    a = new int[5];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100);
    }
  }

  public static void main(String[] args) {
    method04();
  }

  /**
   * cn.hutool.core.util.PrimitiveArrayUtil#reverse(int[])
   */
  private static void method04() {
    System.out.println("数组中的各个随机数是:");
    MyArrayUtil.toString(a);
    ArrayUtil.reverse(a);
    System.out.println("反转后数组中的各个随机数是:");
    MyArrayUtil.toString(a);
  }

  /**
   * org.apache.commons.lang3.ArrayUtils#reverse(int[])
   */
  private static void method03() {
    System.out.println("数组中的各个随机数是:");
    MyArrayUtil.toString(a);
    ArrayUtils.reverse(a);
    System.out.println("反转后数组中的各个随机数是:");
    MyArrayUtil.toString(a);
  }

  /**
   * 使用临时数组,有助于理解
   */
  public static void method02() {
    int[] tempArray = new int[a.length];
    for (int i = a.length - 1; i >= 0; i--) {
      tempArray[i] = a[i];
    }
    System.out.println("临时数组中的各个随机数是:");
    MyArrayUtil.toString(tempArray);
    for (int i = 0; i < a.length; i++) {
      a[i] = tempArray[tempArray.length-1-i];
    }
    System.out.println("反转后数组中的各个随机数是:");
    MyArrayUtil.toString(a);
  }

  /**
   * 此方法是org.apache.commons.lang3.ArrayUtils#reverse(int[])中的方法
   */
  public static void method01() {
    System.out.println("数组中的各个随机数是:");
    MyArrayUtil.toString(a);
    System.out.println("反转后-----------");
    int i = 0;
    for(int j = a.length - 1; j > i; ++i) {
      int tmp = a[j];
      a[j] = a[i];
      a[i] = tmp;
      --j;
    }
    MyArrayUtil.toString(a);
  }


}

三、数组 排序

数组 排序

package com.chenheng.array.c;

import com.chenheng.array.utils.MyArrayUtil;

/**
 * @author:
 * @create: 2021-04-19 13:38
 * @description:
 * 1、选择法排序
 * 2、冒泡法排序
 **/
public class SortArray {
  private static int[] a;

  /**
   * 静态代码块的特点:随着类的加载而执行,而且只执行一次
   */
  static {
    a = new int[5];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100);
    }
  }

  public static void main(String[] args) {
//    selectionSortArray(a);
    bubbleSortArray(a);
  }

  /**
   * 冒泡法排序的思路:
   * 第一步:从第一位开始,把相邻两位进行比较
   * 如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
   * 第二步:再来一次,只不过不用比较最后一位
   * 以此类推
   * @param a
   */
  public static void  bubbleSortArray(int[] a) {
    System.out.println("排序前:");
    MyArrayUtil.toString(a);
    /**
     * 第一步:从第一位开始,把相邻两位进行比较
     * 如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
     */
    for (int i = 0; i < a.length-1-0; i++) {
      if (a[i] > a[i+1]) {
        int temp = a[i];
        a[i] = a[i+1];
        a[i+1] = temp;
      }
    }
    System.out.println("第一轮排序");
    MyArrayUtil.toString(a);
    /**
     * 第二步:再来一次,只不过不用比较最后一位
     */
    for (int i = 0; i < a.length-1-1; i++) {
      if (a[i] > a[i+1]) {
        int temp = a[i];
        a[i] = a[i+1];
        a[i+1] = temp;
      }
    }
    System.out.println("第二轮排序");
    MyArrayUtil.toString(a);
    for (int j = 0; j < a.length; j++) {
      for (int i = 0; i < a.length-j-1; i++) {
        if(a[i]>a[i+1]){
          int temp = a[i];
          a[i] = a[i+1];
          a[i+1] = temp;
        }
      }
    }
    System.out.println("排序后:");
    MyArrayUtil.toString(a);
  }

  /**
   * 选择法排序的思路:
   * 把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来
   * 比较完后,第一位就是最小的
   * 然后再把第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来
   * 比较完后,第二位就是第二小的
   * @param a
   */
  public static void selectionSortArray(int[] a) {
    System.out.println("排序前:");
    MyArrayUtil.toString(a);
    /**
     * 把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来
     */
    for (int i = 0; i < a.length-1; i++) {
      if (a[0] > a[i+1]) {
        int temp = a[0];
        a[0] = a[i+1];
        a[i+1] = temp;
      }
    }
    System.out.println("第一轮排序");
    MyArrayUtil.toString(a);
    /**
     * 然后再把第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来
     */
    for (int i = 1; i < a.length-1; i++) {
      if (a[1] > a[i+1]) {
        int temp = a[1];
        a[1] = a[i+1];
        a[i+1] = temp;
      }
    }
    System.out.println("第二轮排序");
    MyArrayUtil.toString(a);
//    选择排序
    for (int i = 0; i < a.length-1; i++) {
      for (int j = i+1; j < a.length; j++) {
        if (a[i] > a[j]) {
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }
    System.out.println("选择排序第一个:");
    MyArrayUtil.toString(a);
//    选择排序
    for (int i = 0; i < a.length -1 ; i++) {
      for (int j = i+1; j < a.length; j++) {
        swap(a, i, j);
      }
    }
    System.out.println("选择排序第二个:");
    MyArrayUtil.toString(a);
  }

  /**
   * 进行数组中的元素交换
   * @param a int[]
   * @param i 坐标
   * @param j 坐标
   */
  public static void swap(int[] a, int i, int j) {
    if (a[i] > a[j]) {
      int temp = a[j];
      a[j] = a[i];
      a[i] = temp;
    }
  }

}

四、数组 增强型for循环

数组 增强型for循环

package com.chenheng.array.d;

import cn.hutool.core.util.ArrayUtil;
import com.chenheng.array.utils.MyArrayUtil;

/**
 * @author:
 * @create: 2021-04-19 14:29
 * @description: 用增强型for循环找出最大的那个数
 **/
public class EnhancedForArray {
  private static int[] a;

  /**
   * 静态代码块的特点:随着类的加载而执行,而且只执行一次
   */
  static {
    a = new int[5];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100);
    }
  }

  public static void main(String[] args) {
//    traversal();
//    maxValueArray(a);
    maxVaule(a);

  }

  /**
   * cn.hutool.core.util.PrimitiveArrayUtil#max(int...)
   * @param a
   */
  public static void maxVaule(int[] a) {
    MyArrayUtil.toString(a);
    int max = ArrayUtil.max(a);
    System.out.println("最大值:" + max);
  }

  /**
   * 用增强型for循环找出最大的那个数
   * @param a
   */
  public static void maxValueArray(int[] a) {
    MyArrayUtil.toString(a);
    int max = a[0];
    for (int each : a) {
      if (each >= max) {
        max = each;
      }
    }
    System.out.println("最大值:" + max);
  }

  /**
   * 数组遍历
   */
  public static void traversal() {
//    常规遍历
    for (int i = 0; i < a.length; i++) {
      int each = a[i];
      System.out.println(each);
    }
//    增强型遍历
    for (int each : a) {
      System.out.println(each);
    }
  }

}

五、数组 复制数组

数组 复制数组

package com.chenheng.array.e;

/**
 * @author:
 * @create: 2021-04-19 15:40
 * @description: 复制数组
 **/

import cn.hutool.core.collection.CollectionUtil;
import com.chenheng.array.utils.MyArrayUtil;

/**
 * 把一个数组的值,复制到另一个数组中
 * System.arraycopy(src, srcPos, dest, destPos, length)
 * src:源数组
 * srcPos:从源数组复制数据的起始位置
 * dest:目标数组
 * destPos:复制到目标数组的起始位置
 * length:复制的长度
 */
public class CopyArray {

  public static void main(String[] args) {
//    handCopyArray();
    mergeArray();
  }

  /**
   * 练习-合并数组
   * 首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组
   * 然后准备第三个数组,第三个数组的长度是前两个的和
   * 通过System.arraycopy 把前两个数组合并到第三个数组中
   */
  public static void mergeArray() {
    int[] a = new int[(int) (Math.random() * 5) + 5];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100);
    }
    System.out.println("a数组各个元素:");
    MyArrayUtil.toString(a);
    int[] b = new int[(int) (Math.random() * 5) + 5];
    for (int i = 0; i < b.length; i++) {
      b[i] = (int) (Math.random() * 100);
    }
    System.out.println("b数组各个元素:");
    MyArrayUtil.toString(b);
    int[] c = new int[a.length + b.length];
    System.arraycopy(a, 0, c, 0, a.length);
    System.arraycopy(b, 0, c, a.length, b.length);
    System.out.println("c数组各个元素:");
    MyArrayUtil.toString(c);
  }

  private static void handCopyArray() {
    int[] a = new int[]{18,62,68,82,65,9};
    int[] b = new int[3];
//    方法一:for循环
    for (int i = 0; i < b.length; i++) {
      b[i] = a[i];
    }
    MyArrayUtil.toString(b);
    //方法二: System.arraycopy(src, srcPos, dest, destPos, length)
    //src: 源数组
    //srcPos: 从源数组复制数据的起始位置
    //dest: 目标数组
    //destPos: 复制到目标数组的启始位置
    //length: 复制的长度
    System.arraycopy(a, 0, b, 0, 3);
    MyArrayUtil.toString(b);
  }

}

六、数组 二维数组

数组 二维数组

package com.chenheng.array.f;

/**
 * @author:
 * @create: 2021-04-22 20:31
 * @description: 二组数组
 **/
public class TwoDimensionalArray {

  public static void main(String[] args) {
//    initArray();
    exerciseArray();
  }

  /**
   * 练习-二维数组
   * 定义一个5*5的二维数组,然后使用随机数填充该二维数组
   * 找出这个二维数组里,最大的那个值,并打印出其二维坐标
   * 0-100的随机整数的获取办法有多重,下面是参考办法之一:
   * (int) (Math.random() * 100)
   * Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
   */
  public static void exerciseArray() {
    int[][] a = new int[5][5];
    System.out.println(a.length);
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a.length; j++) {
        a[i][j] = (int)(Math.random() * 100);
      }
    }
    System.out.println("打印出二维数组");
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a.length; j++) {
        System.out.print(a[i][j] + "\t");
      }
      System.out.println();
    }
    System.out.println("找出这个二维数组里,最大的那个值,并打印出其二维坐标");
    /**
     * 初始数组最大值
     * 初始数组最大值x坐标
     * 初始数组最大值y坐标
     */
    int max = 0;
    int x = 0;
    int y = 0;
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a[i].length; j++) {
        System.out.print(a[i][j]+"\t");
        if (a[i][j] > max) {
          max = a[i][j];
          x = i;
          y = j;
        }
      }
      System.out.println();
    }
    System.out.println("数组最大值->"+max);
    System.out.println("数组最大值的坐标->"+x+"\t"+y);
  }

  /**
   * 初始化数组
   */
  public static void initArray() {
    /**
     * 初始化二维数组
     */
    /**
     * 有两个一维数组,每个一维数组的长度是3
     */
    int[][] a = new int[2][3];
    /**
     * 可以直接访问一维数组,因为已经分配了空间
     */
    a[1][2] = 5;
    /**
     * 只分配了二维数组
     */
    /**
     * 有两个一维数组,每个一维数组的长度暂未分配
     */
    int[][] b = new int[2][];
    /**
     * 必须事先分配长度,才可以访问
     */
    b[0] = new int[3];
    b[0][5] = 5;
    /**
     * 指定内容的同时,分配空间
     */
    int[][] c = new int[][]{{1,2,4},{4,5},{6,7,8,9}};
  }

}

七、数组 Arrays

数组 Arrays

package com.chenheng.array.g;

import java.util.Arrays;

/**
 * @author:
 * @create: 2021-04-23 10:41
 * @description:Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能
 * 大大提高了开发人员的工作效率。
 **/
public class ExerciseArrays {
  private static int[] a;
  private static int[] b;
  static {
    a = new int[]{18, 62, 68, 82, 65, 9};
    b = new int[]{18, 62, 68, 82, 65, 8};
  }

  public static void main(String[] args) {
//    copyArray();
//    toArrayString();
//    sortArray();
//    searchElement();
//    isSameArray();
//    fillArrayBySameElement();
    sortTwoDimensionalArray();
  }

  /**
   * 练习-二维数组排序
   * 首先定义一个5X8的二维数组,然后使用随机数填充满。
   * 借助Arrays的方法对二维数组进行排序。
   * 参考思路:
   * 先把二维数组使用System.arraycopy进行数组复制到一个一维数组
   * 然后使用sort进行排序
   * 最后再复制回到二维数组。
   */
  public static void sortTwoDimensionalArray() {
    int[][] twoDimensionalArray = new int[5][8];
    System.out.println("初始化数组->");
    for (int i = 0; i < twoDimensionalArray.length; i++) {
      for (int j = 0; j < twoDimensionalArray[i].length; j++) {
        twoDimensionalArray[i][j] = (int)(Math.random() * 100);
        System.out.print(twoDimensionalArray[i][j]+"\t");
      }
      System.out.println();
    }
    System.out.println("-------------------------------------");
    int[] tempArray = new int[40];
    for(int i=0; i<twoDimensionalArray.length; i++){
      System.arraycopy(twoDimensionalArray[i], 0, tempArray, 8*i, twoDimensionalArray[i].length);
    }
    Arrays.sort(tempArray);
    System.out.println("排序后的数组->" + Arrays.toString(tempArray));
    for(int i=0; i<twoDimensionalArray.length; i++)
    {
      for(int j=0;j<twoDimensionalArray[i].length; j++){
        twoDimensionalArray[i][j] = tempArray[twoDimensionalArray[i].length*i+j];
        System.out.print(twoDimensionalArray[i][j] + "\t");
      }
      System.out.println();
    }
  }

  /**
   * 使用同一个值,填充整个数组
   */
  public static void fillArrayBySameElement() {
    int[] needFillArray = new int[10];
    Arrays.fill(needFillArray, 10);
    System.out.println(Arrays.toString(needFillArray));
  }

  /**
   * 比较两个数组的内容是否一样
   * 第二个数组的最后一个元素是8,和第一个数组不一样,所以比较结果是false
   */
  public static void isSameArray() {
    System.out.println(Arrays.equals(a, b));
  }

  /**
   * 查询元素出现的位置
   * 需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序
   * 如果数组中有多个相同的元素,查找结果是不确定的
   */
  public static void searchElement() {
    Arrays.sort(a);
    System.out.println(Arrays.toString(a));
    /**
     * 使用binarySearch之前,必须先使用sort进行排序
     */
    System.out.println("数字9 出现的位置是->"+Arrays.binarySearch(a, 9));
  }

  /**
   * 在前面章节学习了 选择法排序 和 冒泡法排序,
   * Arrays工具类提供了一个sort方法,只需要一行代码即可完成排序功能。
   */
  public static void sortArray() {
    System.out.println("排序前->"+ Arrays.toString(a));
    Arrays.sort(a);
    System.out.println("排序后->"+ Arrays.toString(a));
  }

  /**
   * 如果要打印一个数组的内容,就需要通过for循环来挨个遍历,逐一打印
   *
   * 但是Arrays提供了一个toString()方法,直接把一个数组,转换为字符串,这样方便观察数组的内容
   */
  public static void toArrayString() {
    String content = Arrays.toString(a);
    System.out.println(content);
  }

  /**
   * 与使用System.arraycopy进行数组复制类似的, Arrays提供了一个copyOfRange方法进行数组复制。
   * 不同的是System.arraycopy,需要事先准备好目标数组,并分配长度。 copyOfRange 只需要源数组就就可以了,通过返回值,就能够得到目标数组了。
   * 除此之外,需要注意的是 copyOfRange 的第3个参数,表示源数组的结束位置,是取不到的。
   */
  public static void copyArray() {
    int[] a = new int[]{18, 62, 68, 82, 65, 9};
    /**
     * 第一个参数表示源数组
     * 第二个参数表示开始位置(取得到)
     * 第三个参数表示结束位置(取不到)
     */
    int[] b = Arrays.copyOfRange(a, 0, 3);
    for (int i = 0; i < b.length; i++) {
      System.out.print(b[i] + "\t");
    }
  }

}

八、工具类

package com.chenheng.array.utils;

/**
 * @author:
 * @create: 2021-04-19 13:30
 * @description:
 **/
public class MyArrayUtil {
  public static void toString(int[] array) {
    StringBuilder sb = new StringBuilder();
    int length = array.length;
    for (int i = 0; i < length; i++) {
      if (i == length-1) {
        sb.append(array[i]);
      } else{
        sb.append(array[i] + ",");
      }
    }
    System.out.println(sb);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值