array10.java

来自「已经编写好的数据结构课本程序可以减轻您的负担」· Java 代码 · 共 45 行

JAVA
45
字号
// =============== Program Description ===============
// 程序名称: array10.java
// 程序目的: 将下三角数组转换成以行为主的一维数组
// Written By Kuo-Yu Huang. (WANT Studio.) 
// ===================================================
import java.io.*;

public class array10
{
	public static void main(String args[])
	{
						// 预设5*5的下三角矩阵数据
		int[][] Lower = { {3, 0, 0, 0, 0},
			          {7, 5, 0, 0, 0},
			          {6, 4, 5, 0, 0},
				  {8, 3, 2, 1, 0},
			          {9, 1, 6, 4, 9}};
		int	RowMajor[] = new int[15];	// 存储转换后的数组数据
		int	Index;			// 数组的索引值
		int	i,j;			// 循环计数变量

		System.out.println("Two dimensional Lower triangular array:");

		for (i=0;i<5;i++)		// 打印出下三角数组数据
		{
			for (j=0;j<5;j++)
				System.out.print(" "+Lower[i][j]+" ");
			System.out.println("");
		}

		for (i=0;i<5;i++)		// 进行数组数据转换
			for (j=0;j<5;j++)
				if (i >= j)
				{
					Index = i*(i+1)/2 + j;
					RowMajor[Index] = Lower[i][j];
				}
		System.out.println("");

		System.out.println("Row Major one dimensional array:");
		for (i=0;i<15;i++)		// 打印出转换后的数组数据
			System.out.print(" "+RowMajor[i]+" ");
		System.out.println(" ");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?