我今天学习了多维数组,及一些排序,其内容如下:
二维数组:数组中的每个数组元素又是一维数组。
多维数组不必要是规则矩阵
*/
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");
}
}
}