数组是java中最基本的一种数据结构,它所占的是一块连续的存储空间。
数组既有优点又有缺点。它的优点在于:按照下标顺序或者直接指定的下标,访问读取的速度效率高于其他的数据结构。
而它的缺点则是:内存空间在定义的时候就固定了,不能改变其内存的大小。
java中有两种数据类型:1.基本数据类型;2.引用类型(对象类型,类类型)如:class,interface,abstract class,数组。
数组只有唯一的一个属性那就是length。
一维数组的定义方法基本是以下两种:
1.数据类型 [] 数组名 = new 数据类型[长度];
数据类型 [] 数组名 = {值,...};
2.数据类型 [] 数组名;
数组名 = new 数据类型[长度];
数据类型 [] 数组名;
数组名 = new 数据类型[]{值,...};
二维数组的定义方法基本是以下两种:
1.数据类型 [][] 数组名 = new 数据类型[行][列];
数据类型 [][] 数组名 = {{值,...},...};
2.数据类型 [][] 数组名;
数组名 = new 数据类型[行][列];
数据类型 [] 数组名;
数组名 = new 数据类型[][]{{值,...},...};
关于二维数组的各种长度的获取:
获取二维数组的总行数:数组名.length;
获取二维数组的每一行的列数:数组名[行下标].length;
获取二维数组总的元素个数:
获取某一个元素:数组名[行下标][列下标]
当在一个没有分配内存空间的空对象名字上调用对象的方法或属性,就会出现空指针异常,即定义了变量类型和变量的名字,但这个变量并没有指向一个具体存在的对象,那么这个变量的默认值是null,在调用时就会报错,此为空指针异常。
解决方法:注意new的使用。
package 多维数组;
import java.util.Random;
public class Array {
/**
* 主程序入口
*/
public static void main(String[] args){
//实例化一个Array类对象
Array ay = new Array();
//取得要排序的原数组
int[] array = ay.creatArray(10);
System.out.println("----排序前数组值的顺序:");
//打印数组各个元素值
ay.printArray(array);
System.out.println("----数组排序后的结果:");
//调用插入法给数组排序
ay.charu(array);
//打印数组各个元素值
ay.printArray(array);
}
/**
* 生成一个乱序的,指定长度的原始数组
*/
public static int[] creatArray(int size){
// 实例化一个int类型的数组对象
int[] array = new int[size];
// 实例化一个随机数类的对象
Random random = new Random();
//遍历数组,给数组每一个下标赋值
for(int i = 0; i<array.length; i++){
// 给数组赋上随机值
array[i] = random.nextInt(50);
}
return array;
}
/**
* 用插入法给数组排序
*/
public static int[] charu(int [] array){
for(int i = 1; i<array.length; i++){
for(int j = i; j>0; j--){
if(array[j]<array[j-1]){
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
return array;
}
/**
* 打印出数组中的元素值
*/
public static void printArray(int[] array){
//如果要打印的数组为null,则不打印
if(array==null){
return;
}
//遍历数组输出每一个下标位置的元素
for(int i = 0; i<array.length; i++){
//输出信息
System.out.print(array[i]+"\t");
}
System.out.println();
}
}
运行结果:
----排序前数组值的顺序:
4 39 26 3 7 2 40 6 9 12
----数组排序后的结果:
2 3 4 6 7 9 12 26 39 40
package 多维数组;
import java.util.Random;
public class ArrayT {
/**
* 主程序入口
*/
public static void main(String[] args) {
//实例化一个ArrayT类对象
ArrayT at = new ArrayT();
//取得要排序的原数组
int[][] array = at.createArray(10, 10);
//打印数组各个元素值
at.printArray(array);
//找出最大值,并输出它的个数和位置
at.chazhao(array);
}
/**
* 创建一个二维数组的方法
* @param row
* @param column
* @return
*/
public static int[][] createArray(int row, int column) {
// 实例化一个int类型的数组对象
int[][] array = new int[row][column];
//实例化一个随机数类的对象
Random random = new Random();
// 遍历数组,给数组的每一个下标位置附上值。
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
// 给数组赋随机值
array[i][j] = random.nextInt(50);
}
}
return array;
}
/**
* 打印出数组中的元素值
*/
public static void printArray(int[][] array) {
// 遍历数组,输出每一个下标位置的元素
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
// 输出信息
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
/**
* 找出最大值方法
*/
public static void chazhao(int [][] array){
//查找最大值
int max = array[0][0],a1=0,a2=0,a3=0;
//找出数组中的最大值
for(int i = 0; i<array.length; i++){
for(int j = 0; j<array[i].length; j++){
if(max<array[i][j]){
max=array[i][j];
}
}
}
//判断数组中有多少个这样的最大值,并输出各个值的所在位置
for(int i = 0; i<array.length; i++){
for(int j = 0; j<array[i].length; j++){
if(max==array[i][j]){
a1=i+1;
a2=j+1;
a3++;
System.out.println("最大值为:"+max+"位置在:"+a1+"行"+a2+"列");
}
}
}
System.out.println("最大值共有个数为:"+a3);
}
}
运行结果:
14 41 4 27 9 10 48 46 19 10
46 39 49 5 25 12 13 15 14 27
34 6 3 11 27 21 26 36 18 46
43 11 25 9 28 16 14 7 10 45
16 20 44 23 12 14 32 33 32 7
13 4 3 6 26 4 6 43 32 3
25 17 46 9 0 2 27 7 49 39
26 5 26 32 49 33 34 20 47 41
35 41 24 47 12 17 26 0 13 16
41 26 26 43 27 23 26 13 39 15
最大值为:49位置在:2行3列
最大值为:49位置在:7行9列
最大值为:49位置在:8行5列
最大值共有个数为:3