java二維數組存儲-九游会j9娱乐平台
java中數組是以對象形式存在的,棧中存儲的是數組對象的地址(即引用),而對象本身不存放在棧中,而是存放在堆中,使用時通過棧中的引用地址找到堆中的實際對象。
ⅱ java 的二維數組是按行還是列 存儲的c語言為什麼是按行存儲的,這樣存儲有什麼好處
c和java的多維數據都是按維存儲的,不是行也不是列。比如二維數據是連續存儲一維數據的信息。這樣做的好處就是便於訪問和擴展。
ⅲ 用java聲明matrix類表示矩陣,使用二維數組存儲矩陣元素,實現以下方法:
public class matrix {
private static string matrix_a;
private int mx[][], m, n;
public matrix(int r, int c) {
m = r;
n = c;
mx = new int[m][n];
inimatrix();
}
public matrix() {
m = 3;
n = 3;
mx = new int[3][3];
inimatrix();
}
public void inimatrix()// 隨機取數
{
int i, j;
for (i = 0; i <= m - 1; i )
for (j = 0; j <= n - 1; j )
mx[i][j] = (int) (math.random() * 100);
}
public void tranmatrix()// 轉置矩陣
{
int i, j, t;
int mt[][] = new int[m][n];
for (i = 0; i <= m - 1; i )
for (j = 0; j <= n - 1; j )
mt[i][j] = mx[i][j];
t = m;
m = n;
n = t;
mx = new int[m][n];
for (i = 0; i <= m - 1; i )
for (j = 0; j <= n - 1; j )
mx[i][j] = mt[j][i];
}
public void printmatrix()// 輸出矩陣所有值
{
int i, j;
for (i = 0; i <= m - 1; i ) {
for (j = 0; j <= n - 1; j )
system.out.print(" " mx[i][j]);
system.out.println();
}
}
//判斷一個矩陣是否為上三角矩陣
public boolean isuppertriangularmatrix() {
int i, j = 0;
int c = this.mx[1][0];
for(i=1; i
break;
if(i>=this.mx.length)
return true;
return false;
}
public void addmatrix(matrix b)// 矩陣相加
{
int i, j;
for (i = 0; i <= m - 1; i )
for (j = 0; j <= n - 1; j )
mx[i][j] = mx[i][j] b.mx[i][j];
}
public static void main(string args[]) {
matrix ma = new matrix(4, 3);
matrix mb = new matrix(4, 3);
system.out.println("the matrix_a:");
ma.printmatrix();
system.out.println("the matrix_b:");
mb.printmatrix();
if(ma.isuppertriangularmatrix())
system.out.println("上三角矩陣:\n" ma.isuppertriangularmatrix());
system.out.println("matrix_a matrix_b:");
ma.addmatrix(mb);
ma.printmatrix();
system.out.println("transpose matrix_a:");
mb.tranmatrix();
mb.printmatrix();
system.out.println("transpose matrix_a matrix_b:");
mb.tranmatrix();
mb.printmatrix();
}
}
ⅳ java 中的arraylist如何存放二維數組
一,首先,你這么做的目的是什麼?
二,如果只是想試著玩,想更進一步了解list 跟 數組的用法。
(1)list 里放一維的:list