2021.12.1

这篇博客探讨了Java中的二维数组概念,包括非规则矩阵的创建和初始化,并展示了如何进行数组拷贝。同时,文章还介绍了排序算法的重要性,提到了冒泡排序和选择排序的基本思想,并通过Arrays.sort()方法对数组进行排序。博主强调了排序算法的时间复杂度和稳定性在实际应用中的考量因素。
摘要由CSDN通过智能技术生成

我今天学习了多维数组,及一些排序,其内容如下:


    二维数组:数组中的每个数组元素又是一维数组。
                多维数组不必要是规则矩阵
*/ 


public class TwoArray{
    public static void main(String[] args){
        /*int[] array1 = new int[5];
        //定义二维数组
        int[][] array2 = new int[3][];
        //定义二维数组中的每个一维数组的大小
        array2[0] = new int[2];
        array2[1] = new int[4];
        array2[2] = new int[3];
        
        //为一维数组中的每个元素赋值
        array2[0][0] = 1;
        array2[0][1] = 2;
        
        array2[1][0] = 10;
        array2[1][1] = 20;
        array2[1][2] = 30;
        array2[1][3] = 40;
        
        array2[2][0] = 100;
        array2[2][1] = 200;
        array2[2][2] = 300;
        
        //输出二维数组的每个值
        for(int i = 0; i <array2.length;i++){
            for(int j = 0; j <array2[i].length; j++ ){
                System.out.print(array2[i][j] + "\t");
            }
        }*/
        
        //数组的拷贝
        /*String[] s = {"Mircosoft","IBM","Sun","Oracle","Apple"};
        String[] sBak = new String[6];
        System.arraycopy(s,0,sBak,0,s.length);
        for(int i=0;i<sBak.length;i++){
            System.out.print(sBak[i]+" ");
        }*/
        /*System.out.println();
        int[][] intArray = {{1,2},{1,2,3},{3,4}};
        int[][] intArrayBak = new int[3][];
        System.arraycopy(intArray,0,intArrayBak,0,intArray.length);
        intArrayBak[2][1] = 100;
        for(int i = 0;i<intArrayBak.length;i++){
            for(int j =0;j<intArrayBak[i].length;j++){
                System.out.print(intArrayBak[i][j]+" ");
            }
            System.out.println();
        }*/
        
        
    }
}

import java.util.Arrays;
/*
数组也是数据结构中的一种实现,在存储数据的时候经常用数组来存储
    经常见的数据结构:
            线性表
            非线性表
            树
            图
            队列
            堆
            栈
    
数组经常用来考算法:    
    面试需求:
            1、写出某个算法
                冒泡排序
                选择排序
                插入排序
                快速排序
            2、排序算法的时间复杂度(空间复杂度)
                衡量一个数据结构是否是合适的衡量标准
            3、排序算法的稳定性            
                排序之前数组的元素位置和排序之后的数组元素位置是否发生变化
        
*/

public class ArraySort{
    public static void main(String[] args){
         //给定一个数组,从小到大进行排序
         int[] array = new int[]{1,3,5,7,2,9,8,4,6};
         /*
         冒泡排序
         for(int i = 0 ; i<array.length; i++ ){
             for(int j = 0; j <array.length-1-i; j++){
                 if(array[j] > array[j+1]){
                     int temp = array[j];
                     array[j] = array[j+1];
                     array[j+1] = temp;
                 }
             }
         }*/
         
         //选择排序
         /*for(int i = 0; i<array.length;i++){
             //记录假定最小的值的位置
             int index = i;
             for(int j = i; j<array.length ; j++){
                 if(array[j] < array[index]){
                     index = j;
                 }
             } 
             //交换位置,交换假定最小的和真实最小的位置
             int temp = array[index];
             array[index] = array[i];
             array[i] = temp;
         }*/
         //使用Arrays的sort方法进行排序
         Arrays.sort(array);
         
         //遍历数组
         for(int i = 0;i<array.length; i++){
             System.out.print(array[i] + "\t");
         }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值