黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java初始化二維數(shù)組的方法

Java初始化二維數(shù)組的方法

更新時(shí)間:2022-06-08 10:46:46 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2093次

動(dòng)力節(jié)點(diǎn)小編來告訴大家Java初始化二維數(shù)組的方法。多維數(shù)組稱為多維數(shù)組,最常用的多維數(shù)組是 2-D 和 3-D 數(shù)組。我們可以說任何高維數(shù)組基本上都是數(shù)組的數(shù)組。二維數(shù)組的一個(gè)非常常見的例子是棋盤。棋盤是一個(gè)包含 64 個(gè) 1×1 方格的網(wǎng)格。您可以類似地可視化二維數(shù)組。在二維數(shù)組中,每個(gè)元素都與行號(hào)和列號(hào)相關(guān)聯(lián)。訪問二維數(shù)組的任何元素類似于使用行號(hào)和列號(hào)訪問 Excel 文件的記錄。二維數(shù)組在實(shí)現(xiàn)井字游戲、國(guó)際象棋甚至存儲(chǔ)圖像像素時(shí)很有用。

在 Java 中聲明二維數(shù)組:

任何二維數(shù)組都可以聲明如下:

句法:

數(shù)據(jù)類型數(shù)組名[][];(OR) 數(shù)據(jù)類型[][] 數(shù)組名稱;

data_type:因?yàn)镴ava 是一種靜態(tài)類型的語言(即,它希望變量在被賦值之前被聲明)。因此,指定數(shù)據(jù)類型決定了它將接受的元素類型。例如,僅存儲(chǔ)整數(shù)值,數(shù)據(jù)類型將被聲明為 int。

array_name:它是賦予二維數(shù)組的名稱。例如科目、學(xué)生、水果、部門等。

注意:我們可以在 data_type 之后寫 [ ][ ] 或者我們可以在聲明 2D 數(shù)組時(shí)在 array_name 之后寫 [ ][ ]。

// java program showing declaration of arrays
import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		int[][] integer2DArray; // 2D integer array
		String[][] string2DArray; // 2D String array
		double[][] double2DArray; // 2D double array
		boolean[][] boolean2DArray; // 2D boolean array
		float[][] float2DArray; // 2D float array
		double[][] double2DArray; // 2D double array
	}
}

Java中二維數(shù)組的初始化:

data_type[][] array_Name = new data_type[no_of_rows][no_of_columns];

任何二維數(shù)組中的總元素將等于 (no_of_rows) * (no_of_columns)。

no_of_rows:數(shù)組可以存儲(chǔ)的行數(shù)。例如 no_of_rows = 3,那么數(shù)組將有三行。

no_of_columns:數(shù)組可以存儲(chǔ)的行數(shù)。例如 no_of_columns = 4,那么數(shù)組將有四列。

數(shù)組初始化的上述語法將根據(jù)指定的數(shù)據(jù)類型為所有數(shù)組元素分配默認(rèn)值。

讓我們看看初始化二維數(shù)組的各種方法:

方法一

// java program to initialize a 2D array
import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		// Declaration along with initialization
		// 2D integer array with 5 rows and 3 columns
		// integer array elements are initialized with 0
		int[][] integer2DArray = new int[5][3];
		System.out.println(
			"Default value of int array element: "
			+ integer2DArray[0][0]);
		// 2D String array with 4 rows and 4 columns
		// String array elements are initialized with null
		String[][] string2DArray = new String[4][4];
		System.out.println(
			"Default value of String array element: "
			+ string2DArray[0][0]);
		// 2D boolean array with 3 rows and 5 columns
		// boolean array elements are initialized with false
		boolean[][] boolean2DArray = new boolean[4][4];
		System.out.println(
			"Default value of boolean array element: "
			+ boolean2DArray[0][0]);
		// 2D char array with 10 rows and 10 columns
		// char array elements are initialized with
		// '\u0000'(null character)
		char[][] char2DArray = new char[10][10];
		System.out.println(
			"Default value of char array element: "
			+ char2DArray[0][0]);
		// First declaration and then initialization
		int[][] arr; // declaration
		// System.out.println("arr[0][0]: "+ arr[0][0]);
		// The above line will throw an error, as we have
		// only declared the 2D array, but not initialized
		// it.
		arr = new int[5][3]; // initialization
		System.out.println("arr[0][0]: " + arr[0][0]);
	}
}

注意:初始化二維數(shù)組時(shí),必須始終指定第一個(gè)維度(行數(shù)),但可以省略提供第二個(gè)維度(列數(shù))。

在下面的代碼片段中,我們沒有指定列數(shù)。但是,Java 編譯器足夠聰明,可以通過檢查列內(nèi)元素的數(shù)量來控制大小。

import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		// The line below will throw an error, as the first
		// dimension(no. of rows) is not specified
		int[][] arr = new int[][3];
		// The line below will execute without any error, as
		// the first dimension(no. of rows) is specified
		int[][] arr = new int[2][];
	}
}

您可以使用行號(hào)和列號(hào)訪問二維數(shù)組的任何元素。

方法二

在下面的代碼片段中,我們沒有指定行數(shù)和列數(shù)。但是,Java 編譯器足夠聰明,可以通過檢查行和列中元素的數(shù)量來控制大小。

import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		String[][] subjects = {
			{ "Data Structures & Algorithms",
			"Programming & Logic", "Software Engineering",
			"Theory of Computation" },					 // row 1
			{ "Thermodynamics", "Metallurgy",
			"Machine Drawing",
			"Fluid Mechanics" },							 // row2
			{ "Signals and Systems", "Digital Electronics",
			"Power Electronics" }						 // row3
		};
		System.out.println(
			"Fundamental Subject in Computer Engineering: "
			+ subjects[0][0]);
		System.out.println(
			"Fundamental Subject in Mechanical Engineering: "
			+ subjects[1][3]);
		System.out.println(
			"Fundamental Subject in Electronics Engineering: "
			+ subjects[2][1]);
	}
}

方法三

此外,我們可以分別初始化數(shù)組的每個(gè)元素。看下面的代碼片段:

import java.io.*;
import java.util.*;
class GFG {
	public static void main(String[] args)
	{
		int[][] scores = new int[2][2];
		// Initializing array element at position[0][0],
		// i.e. 0th row and 0th column
		scores[0][0] = 15;
		// Initializing array element at position[0][1],
		// i.e. 0th row and 1st column
		scores[0][1] = 23;
		// Initializing array element at position[1][0],
		// i.e. 1st row and 0th column
		scores[1][0] = 30;
		// Initializing array element at position[1][1],
		// i.e. 1st row and 1st column
		scores[1][1] = 21;
		// printing the array elements individually
		System.out.println("scores[0][0] = "
						+ scores[0][0]);
		System.out.println("scores[0][1] = "
						+ scores[0][1]);
		System.out.println("scores[1][0] = "
						+ scores[1][0]);
		System.out.println("scores[1][1] = "
						+ scores[1][1]);
		// printing 2D array using Arrays.deepToString() method
		System.out.println(
			"Printing 2D array using Arrays.deepToString() method: ");
		System.out.println(Arrays.deepToString(scores));
	}
}

方法四

如果二維數(shù)組的大小太大,使用上述方法進(jìn)行數(shù)組初始化將是一項(xiàng)繁瑣的任務(wù)。在大型二維數(shù)組的情況下,有效的方法是使用 for 循環(huán)來初始化數(shù)組元素。

import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		int rows = 80, columns = 5;
		int[][] marks = new int[rows][columns];
		// initializing the array elements using for loop
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				marks[i][j] = i + j;
			}
		}
		// printing the first three rows of marks array
		System.out.println("First three rows are: ");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.printf(marks[i][j] + " ");
			}
			System.out.println();
		}		
	}
}

我們可以使用 arr。length 可用于查找行的大小(第 1 維),而 arr[0].length 可用于查找列的大小(第 2 維)。

方法五:(鋸齒狀數(shù)組)

在某些情況下,您希望每一行都有不同數(shù)量的列。這種類型的數(shù)組稱為鋸齒狀數(shù)組。

import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		// declaring a 2D array with 2 rows
		int jagged[][] = new int[2][];
		// not specifying the 2nd dimension,
		// and making it as jagged array
		// first row has 2 columns
		jagged[0] = new int[2];
		// second row has 2 columns
		jagged[1] = new int[4];
		// Initializing the array
		int count = 0;
		for (int i = 0; i < jagged.length; i++) {
			// remember to use jagged[i].length instead of
			// jagged[0].length, since every row has
			// different number of columns
			for (int j = 0; j < jagged[i].length; j++) {
				jagged[i][j] = count++;
			}
		}
		// printing the values of 2D Jagged array
		System.out.println("The values of 2D jagged array");
		for (int i = 0; i < jagged.length; i++) {
			for (int j = 0; j < jagged[i].length; j++)
				System.out.printf(jagged[i][j] + " ");
			System.out.println();
		}
	}
}

執(zhí)行:

讓我們看一個(gè)添加兩個(gè)二維數(shù)組的簡(jiǎn)單程序:

import java.io.*;
import java.util.*;
class GFG {
	public static void main(String[] args)
	{
		int[][] arr1 = { { 1, 2, 3 }, { 4, 5, 6 } };
		int[][] arr2 = { { 4, 5, 6 }, { 1, 3, 2 } };
		int[][] sum = new int[2][3];
		// adding two 2D arrays element-wise
		for (int i = 0; i < arr1.length; i++) {
			for (int j = 0; j < arr1[0].length; j++) {
				sum[i][j] = arr1[i][j] + arr2[i][j];
			}
		}		
		System.out.println("Resultant 2D array: ");
		for (int i = 0; i < sum.length; i++) {
			System.out.println(Arrays.toString(sum[i]));
		}
	}
}

以上就是關(guān)于“Java初始化二維數(shù)組的方法”介紹,如果大家對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的課程內(nèi)容從入門到精通,細(xì)致全面,很適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對(duì)大家能夠有所幫助。

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 99精品国产自在现线观看 | 九九久久国产精品 | 夭天曰天天躁天 | 一本大道香一蕉久在线影院 | 免费看v片网站 | 亚洲mv日韩mv欧美mv | 狠狠色丁香婷婷久久综合不卡 | 免费国产小视频在线观看 | 羞羞视频在线免费 | 黄网站色视频免费观看w | h成年动漫同人网站免费 | 美女视频黄的免费视频网页 | 日批视频免费看 | 中文字幕1区2区 | hd性videos意大利精品 | 手机在线观看你懂得 | ts人妖另类国产 | 成人涩涩网站 | 天天舔天天干 | 日韩高清一区 | 成年网站免费入口在线观看 | 天天射视频 | 天天操天天干天搞天天射 | 羞羞视频在线免费看 | 日本天堂在线 | 97碰视频人人做人人爱欧美 | 日韩一区三区 | 成年人免费在线观看网站 | 九九九九九九伊人 | 欧美视频精品在线 | 一级毛片短视频 | 清清草视频在线 | 天天躁日日躁狠狠躁欧美日韩 | 白丝丝袜高跟国产在线视频 | 一级免费黄色片 | 国产精品一卡二卡三卡 | 在线观看日韩一区 | 一本一道dvd在线播放器 | 一级床上爽高清播放 | 曰批免费视频播放在线看片二 | 性性影院在线观看 |