Java第三章-数组

Java第三章-数组一 目标 1 数组的基本概念及作用 2 数组的创建 3 数组的访问与迭代 4 数组排序 5 二维数组 二 数组的基本概念及作用 数组的基本概念及作用 数组是相同数据类型元素的集合 数组本身是引用数据类型 即对象 但是数组可以存储基本数据类型 也可以存储引用数据类型 三 数组的创建 数组创建的三种方式

大家好,我是讯享网,很高兴认识大家。

一、目标

1、数组的基本概念及作用
2、数组的创建
3、数组的访问与迭代
4、数组排序
5、二维数组

二、数组的基本概念及作用

数组的基本概念及作用 • 数组是相同数据类型元素的集合 • 数组本身是引用数据类型,即对象。但是数组可以存储基本数据类型, 也可以存储引用数据类型。

三、数组的创建

● 数组创建的三种方式:
● 声明数组的同时,根据指定的长度分配内存,但数组中元素值都为默认 的初始化值
int[] ary0 = new int[10];
● 声明数组并分配内存,同时将其初始化
int[] ary1 = new int[]{1, 2, 3, 4, 5};
● 与前一种方式相同,仅仅只是语法相对简略
int[] ary2 = {1, 2, 3, 4, 5};
● 从另一个角度,数组创建可以分为动态和静态两种
• 动态创建数组(没有为元素赋值,可以结合for循环进行赋值)
char[] chAry = new char[10];
• 静态创建数组,在创建的时候,即为每个元素赋初值
int[] ary1 = new int[]{1, 2, 3, 4, 5};
● 数组的长度:length属性
int [] b1 = new int []{1,2,3,4,5,6,7};
System.out.println(b1.length)

import java.util.Arrays; public class JavaArrays1 { 
    public static void main(String[] args) { 
    int []a=new int [10]; System.out.println(Arrays.toString(a)); int []b=new int []{ 
   1,2,3,4,5}; System.out.println(Arrays.toString(b)); int []c={ 
   1,2,3,4,5}; System.out.println(Arrays.toString(c)); int []d=new int [5]; for(int i=0;i<5;i++){ 
    d[0]=1; d[1]=2; } System.out.println(Arrays.toString(d)); int []e={ 
   1,2,3,4,5}; // e[0]=3; // e[1]=2; // System.out.println(Arrays.toString(e)); for(int t:e) { 
    System.out.print(t); } } } 
讯享网

四、数组的访问与迭代

● 数组元素的访问:
• 数组名字[索引] 例如:a[0],a[1];
• 注意:
• 数组的索引从0开始。
• 索引的数据类型是整型 • 索引最大值和数组长度始终差1
数组迭代的两种方式:
第一种:for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
for(int i =0;i<b1.length;i++){
System.out.println(b1[i]);
}

第二种:增强for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
for(数组元素的类型 临时变量名字 :数组的名字){
System.out.println(临时变量名字 );
}

即:
for(int x:b1){
System.out.println(x);
}

讯享网import java.util.Arrays; public class Demo1 { 
    public static void main(String[] args) { 
    int[] a = new int[]{ 
    5, 11, 15, 24, 36, 47, 59, 66,3 }; for(int i=0;i<a.length;i++){ 
    for(int j=0;j<a.length-1;j++){ 
    if(i<j){ 
    int temp=a[j]; a[j]=a[i]; a[i]=temp; } } } System.out.println(Arrays.toString(a)); } } 

五、数组排序

1、冒泡排序

import java.util.Arrays; //冒泡排序 public class SortDemo1 { 
    public static void main(String[] args) { 
    int [] a={ 
   4,5,3,2,1}; //4 3 2 1 5 //3 2 1 4 5 //1 2 3 4 5 for( int i=0;i<a.length-1;i++){ 
    for(int j=0;j<a.length-1-i;j++) { 
    if(a[j]>a[j+1]) { 
    int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } System.out.println(Arrays.toString(a)); } } 

2、选择排序

讯享网import java.util.Arrays; //选择排序1 public class SortDemo2 { 
    public static void main(String[] args) { 
    int []a={ 
   2,3,1,5,6}; //1、3、2、5、6 //1 2 3 5 6 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(Arrays.toString(a)); } } 
import java.util.Arrays; //选择排序2 public class SortDemo3 { 
    public static void main(String[] args) { 
    int[] a = { 
   2, 1, 6, 5, 3}; for (int i = 0; i < a.length - 1; i++) { 
    int minIndex=i; for (int j = i + 1; j < a.length; j++) { 
    if(a[minIndex]>a[j]){ 
    minIndex=j; } } int temp=a[i]; a[i]=a[minIndex]; a[minIndex]=temp; } System.out.println(Arrays.toString(a)); } } 

3、插入排序

import java.util.Arrays; public class SortDemo4 { 
    //插入排序 public static void main(String[] args) { 
    int []a={ 
   2,1,6,4,3}; for(int i=1;i< a.length;i++){ 
    for(int j=i;j>0;j--){ 
    if(a[j]<a[j-1]){ 
    int temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } } } System.out.println(Arrays.toString(a)); } } 
import java.util.Arrays; public class SortDeno5 { 
    public static void main(String[] args) { 
    int []a={ 
   2,1,6,4,3}; int currentvalue=0; for(int i=0;i<a.length-1;i++){ 
    currentvalue =a[i+1]; int preIndex=i; while (preIndex>= 0&& currentvalue <a[preIndex]) { 
    a[preIndex+1] =a[preIndex]; preIndex--; } a[preIndex+1]=currentvalue; } System.out.println(Arrays.toString(a)); } } 

六、二维数组

1、二维数1组的定义:
数组的数组—二维数组的每一个元素是一个一维数 组例如:
int [][]a = { {1,2,3},{1,2,3},{1,2,3}};
2、二维数组的声明:
int [][] a;
int a2[][];

3、数组创建
int [][]a = new int[][]{ {1,2,3},{1,2,3},{1,2,3}};
int [] [] b = { {1,2,3},{1,2,3},{1,2,3}};
int [][] c = new int[3][5];

● int[][] arr = new int[3][5]; ---定义了一个整型的二维数组 ,这个二维数组有3 个一维数组,每一个一维数组包含5个元素. 

4、二维数组的迭代:

public class SortDemo6 { 
    public static void main(String[] args) { 
    int [][]a=new int[3][3]; System.out.println(a[1][1]); int [][]b=new int [3][]; b[0]=new int b[2]; b[1]=new int b[3]; b[2]=new int b[4]; System.out.println(Arrays.toString(b)); } } 
public class SortDemo7 { 
    public static void main(String[] args) { 
    int[][] a = new int[][]{ 
   { 
   1, 2, 3}, { 
   4, 5, 6}, { 
   7, 8, 9}}; for (int i = 0; i < a.length; i++) { 
    for (int j = 0; j < a[i].length; j++) { 
    System.out.print(a[i][j] + "\t"); } System.out.println(); } } } 
小讯
上一篇 2025-04-10 18:06
下一篇 2025-03-06 16:22

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/31328.html