日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ 三维数组 初始化_013 JAVA 多维数组及数组的拷贝、冒泡排序、二分法查找...

發布時間:2025/3/15 c/c++ 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ 三维数组 初始化_013 JAVA 多维数组及数组的拷贝、冒泡排序、二分法查找... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.多維數組

多維數組的元素又是數組,可以有二維、三維、甚至更多維數組

1.1二維數組的聲明:

數據類型 [][] = new 數據類型[一維長度][二維長度]

public class Test01 {public static void main(String[] args) {//一維數組的靜態初始化 數據類型[] 數組名 = {值1,值2,...};int [] arr1 = {11,22,33,44};int [] arr2 = new int[4];arr2[0] = 44;arr2[1] = 33;arr2[2] = 22;arr2[3] = 11;System.out.println("arr1:"+arr1+"tarr2:"+arr2);//二維數組int [][] arrA= new int[2][];arrA[0] = arr1;//引用arrA[1] = arr2;System.out.println("arrA:"+arrA);} }

1.2 二維數組的初始化:(動態初始化和靜態初始化)

public class Test02 {public static void main(String[] args) {//二維數組不是規則的矩陣//二維數組的靜態初始化 數據類型[][] 數組名 = new{{值1,值2,...},{值1,值2,...},{值1,...}...}int [][] arrA = {{1,2},{12,13,14},{1,2,3,4,5}};System.out.println(arrA);//聲明一個二維數組,存儲3個一維數組,每個一維數組長度不清楚int [] arrB[] = new int[3][];arrB[0] = new int[2];// 2個 nullarrB[1] = new int[]{1,2,3,4};arrB[2] = new int[3];System.out.println(arrB);//聲明一個二維數組,同時創建出一維數組,每個一維數組長度相同int arrC[][] = new int[3][4];//存儲3個一維數組,每個一維數組長度為4System.out.println(arrC);} }

1.3 數組的遍歷:

1)普通for循環

2)加強for循環

3)普通for循環+加強for循環

public class Test03 {public static void main(String[] args) {int [][] arrA = {{1,2},{2,3,4},{1,2,3,4,5}};//普通for循環for(int i=0;i<3;i++) {for(int j=0;j<arrA[i].length;j++) {System.out.print(arrA[i][j]+"t");}System.out.println();}System.out.println("--------####----------------");//加強for循環for(int[] arr:arrA) {for(int i:arr) {System.out.print(i+"t");}System.out.println();}System.out.println("-----------****--------------");//普通for循環與加強for循環混搭for(int[] arr:arrA) {//加強forfor(int i=0;i<arr.length;i++) {//普通forSystem.out.print(i+"t");}System.out.println();}System.out.println("===================&*&========");//for(int i=0;i<arrA.length;i++) {//普通forfor(int j:arrA[i]) {//加強forSystem.out.print(j+"t");}System.out.println();}} }

1.4 數組的實例實現:(楊輝三角)---------數組及循環嵌套

public class YangHui {public static void main(String[] args) {int [][] arr = new int[6][6];for(int i=0;i<arr.length;i++) {arr[i][0] = 1;//第一列數字為1arr[i][i] = 1;//對角線元素為1}//其它各元素的值for(int i=2;i<arr.length;i++) { // for(int j=1;j<arr.length;j++) {for(int j=1;j<arr[i].length;j++) {arr[i][j] = arr[i-1][j] + arr[i-1][j-1];}}//遍歷數組,進行打印for(int i=0;i<arr.length;i++) { // for(int j=0;j<arr[i].length;j++) {for(int j=0;j<i;j++) {System.out.print(arr[i][j]+"t");}System.out.println();}} }

1.5 數組存儲表格數據

public class Person {private String name;private int age;private char sex;//構造函數public Person(String name, int age, char sex) {super();this.name = name;this.age = age;this.sex = sex;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn name+"t"+age+"t"+sex;}public static void main(String[] args) {//創建Person類型的數組,用于存儲3個Person類型的對象,用于封裝信息Person[] pers = new Person[3];//創建Person類的對象Person p1 = new Person("張三",37,'男');pers[0] = p1;//將p1對象存儲到Person類型的數組中去pers[1] = new Person("王五",16,'男');pers[2] = new Person("李梅",15,'女');for(int i=0;i<pers.length;i++) {System.out.println(pers);//對象數組中存儲的是對象的引用(內存地址)System.out.println(pers[i]);}}}

內存分析:

1.6 數組的拷貝

在System類里也包含了一個

static void arraycopy(object src,int srcpos,object dest, int destpos,int length)方法,

該方法可以將src數組里的元素值賦給dest數組的元素,其中srcpos指定從src數組的第幾個元素開始賦值,length參數指定將src數組的多少個元素賦給dest數組的元素

public class Test03 {public static void main(String[] args) {int [] arrA = {11,22,33,44};int [] arrB = new int[5];System.out.println("數組拷貝之前:");for(int num:arrB) {System.out.print(num+"t");}System.arraycopy(arrA, 0, arrB, 0, 4);System.out.println("n數組拷貝之后:");for(int num:arrB) {System.out.print(num+"t");}} }

另外,數組的拷貝方法還有:

a.引用(地址)的拷貝

public class Test01 {public static void main(String[] args) {int[] arrA ={11,22,33,44};int [] arrB = new int[5];//拷貝地址(引用)之前System.out.println("拷貝之前:");System.out.println("arrA:"+arrA);System.out.println("arrB:"+arrB);//拷貝地址之后arrB = arrA;System.out.println("拷貝之后:");System.out.println("arrA:"+arrA);System.out.println("arrB:"+arrB);}}

b. 值的拷貝

public class Test02 {public static void main(String[] args) {int [] arrA = {11,22,33,44};int [] arrB = new int[5];//賦值 // arrB[0] = arrA[0]; // arrB[1] = arrA[1]; // arrB[2] = arrA[2]; // arrB[3] = arrA[3];System.out.println("賦值之前:");for(int i=0;i<arrB.length;i++) {System.out.print(arrB[i]+"t");}//賦值int length = Math.min(arrA.length,arrB.length);for(int i=0;i<length;i++) {arrB[i] = arrA[i];}System.out.println("n賦值之后");for(int i=0;i<arrB.length;i++) {System.out.print(arrB[i]+"t");}} }

c. Arrays.copyOf方法,如下:

1.7 java.util.Arrays類

在java.util.Arrays類中,包含了常用的數組操作(排序、查找、填充、打印內容等)

package com.sxt.arrays;import java.util.Arrays;public class Test01 {public static void main(String[] args) {int[] arrA = {11,16,6,154,36};int[] arrB = {11,16,6,154,36};//Arrays.toString 打印數組元素System.out.println(Arrays.toString(arrA));System.out.println("------------------------");System.out.println(arrA.equals(arrB));//比較引用地址//Arrays.equals 比較內容是否一致System.out.println(Arrays.equals(arrA,arrB));//比較內容System.out.println("=========================");int [] arrC = new int[5];System.out.println("拷貝前:"+arrC);System.out.println("-------------------------");//Arrays.copyOf 開辟新的數組來復制指定的數組arrC = Arrays.copyOf(arrA, 7);System.out.println("拷貝后:"+arrC);System.out.println(Arrays.toString(arrC));System.out.println("=========================");//Arrays.fill 將數組中所有元素都已指定元素填充Arrays.fill(arrC, 88);System.out.println(Arrays.toString(arrC));System.out.println("--------------------------");//sort 將數組中元素按照升序進行排序Arrays.sort(arrA);System.out.println("arrA的升序排列:"+Arrays.toString(arrA));System.out.println("==============");int [] arr = {5,13,19,21,37,56,64,75,80,88,92};//折半查找并返回找到元素的下標;未找到則返回值(-插入點-1)System.out.println(Arrays.binarySearch(arr,21));System.out.println(" ");//對Person類型的數組進行排序Person [] pers = new Person[3];pers[0] = new Person("張弎",29,'男');pers[1] = new Person("張大",19,'女');pers[2] = new Person("張五",49,'男');System.out.println("按照年齡的升序排列:");Arrays.sort(pers);for(Person p:pers) {System.out.println(p);}} }class Person implements Comparable<Person> {private String name;private int age;private char sex;//構造函數public Person(String name, int age, char sex) {super();this.name = name;this.age = age;this.sex = sex;}@Overridepublic String toString() {return name+"t"+age+"t"+sex;}@Overridepublic int compareTo(Person o) {//大于 正數,小于負數,等于0return this.age -o.age ;} }

2. 冒泡排序及冒泡排序的優化

2.1冒泡排序算法大致思路:

1. 比較相鄰的元素。如果第一個比第二個大,就交換順序;

2. 對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最后一對。在這一點,最后的元素應該會是最大的數;

3. 每一趟循環都從數列的第一個元素開始比較,依次比較相 鄰的兩個元素,比較到數列的最后;

4. 循環往復,即可得到升序的數組。

圖示:

package com.sxt.bubble;import java.util.Arrays;//冒泡排序 public class Bubble01 {public static void main(String[] args) {int [] arr = {45,13,56,94,16,2,90};System.out.println("排序前:"+Arrays.toString(arr));for(int i=0;i<arr.length-1;i++) {//比較的輪數for(int j=0;j<arr.length-1;j++) {//相鄰兩數比較if(arr[j]>arr[j+1]) {//通過變量交換int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}System.out.println("排序后:"+Arrays.toString(arr));} }

2.2 冒泡排序的優化算法

將整個數組分成兩部分:有序數列和無序數列,因此:

1.不必每一趟都比較到數組結束,到有序數列即可

2.定義一個 boolean 類型的變量 flag,默認有序 true;發生交換,置為 false,一趟循環結束后,根據 flag 的值判斷是否有序,有序,則退出循環

3.設置成員變量,放在大循環外部,不必每次都開辟空間

圖例:

package com.sxt.bubble;import java.util.Arrays;//冒泡排序的優化 public class Bubble02 {public static void main(String[] args) {int [] arr = {2,13,16,56,45,90,94};int temp;boolean flag;int count = 0;System.out.println("排序前:"+Arrays.toString(arr));for(int i=0;i<arr.length-1;i++) {//比較的輪數flag = true;//默認有序count++;//統計比較的次數for(int j=0;j<arr.length-1-i;j++) {//不需要比較到數組的最后,只需要比較到無序部分即可//相鄰兩數比較if(arr[j]>arr[j+1]) {//通過變量交換temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;flag = false;//數組中的元素無序,發生了交換}}if(flag) {System.out.println(flag);break;//數組有序,退出循環,證明數組有序}}System.out.println("排序后:"+Arrays.toString(arr));System.out.println("一共比較了"+count+"輪");} }

3.折半查找(二分法)

總結

以上是生活随笔為你收集整理的c++ 三维数组 初始化_013 JAVA 多维数组及数组的拷贝、冒泡排序、二分法查找...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。