A. java随机生成一个5行六列的二维数组,怎么行列转换
<pre t="code" l="java">思路:
1、首先声明一个6行5列的数组
2、先循环行,再循环列,再赋值为随机数(使用 Random 类)
3、再遍历
代码如下:
public static void main(String[] args) {
//声明一个6行5列的数组
int[][] array=new int[6][5];
for(int i=0,j=array.length;i<j;i++){
for(int h=0,k=array[i].length;h<k;h++){
array[i][h]=new Random().nextInt(100); //赋值:100以内的随机数
}
}
//遍历
for(int i=0,j=array.length;i<j;i++){
System.out.println();
for(int h=0,k=array[i].length;h<k;h++){
System.out.print(array[i][h]+"\t");
}
}
}
B. Java将二维数组的行列元素进行转换
假设,原数组为a[],再声明一个数组b[]。m,n分别为原数组的行和列。
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
b[j][i] = a[i][j];
}
}
C. java程序设计 为每一名雇员计算一周的工作时间 数组存储7名员工的工作时间,以降序显示每个雇员的总时间
public class TestEmploee {
private static int[][] emploees ;
private static int[][] results = new int[7][2];
private static int emploeeIndex = 0;
private static int totalWorkTimeIndex = 1;
public static void main(String[] args){
init();
sumWorkTime();
sortByWorkTime();
printResult();
}
private static void printResult() {
for(int row = 0 ; row < results.length; row++ ){
System.out.println("emploee " + results[row][emploeeIndex] + " : " + " work time is : " + results[row][totalWorkTimeIndex] );
}
}
//排序
private static void sortByWorkTime() {
//冒泡法排序
for(int row = 0 , flag = 0 ; row < results.length ; row ++, flag ++ ){
for( int index = results.length -1 ; index >flag ; index -- ){
if(results[index][totalWorkTimeIndex] > results[index-1][totalWorkTimeIndex]){
int[] temp = results[index-1] ;
results[index-1] = results[index];
results[index] = temp;
}
}
}
}
//计算工作时间
private static void sumWorkTime() {
for(int row = 0 ; row < emploees.length ; row++ ){
for(int col = 0 ; col < emploees[row].length ; col++ ){
results[row][totalWorkTimeIndex]+=emploees[row][col];
}
results[row][emploeeIndex] = row;
}
}
//初始化emploee数组
private static void init() {
emploees = new int [][]{ {4,3,4,5,4},{9,3,5,3,7},{8,4,7,2,4},{5,1,3,7,7},{4,3,5,7,2},{2,1,5,3,8},{8,6,4,3,2} };
}
}
D. java 数组行列转换
你这个的程序,只能成功运行 n*n的情况,也就是行数和列数相等的二维数组才能成功。
如果 n*m 也就是行数和列数不相同的情况就可能出错。
因为 n*m 是没有对角线的,也就是 i = j不一定出现在对角线上,所以出现了你上面的问题。
E. 用java怎样将一个文件中特定行列的数据读取出来
你可以这样组织数据,在列名的行前面加个标志符,用于表明是列名:
#Number Name A B Value;
当读到某行是以#开始的,表明这个不是数据,而是列名,然后把读取的列存放到一个数组中,你可以在列名之间使用分隔符,比如:
# Number : Name : A : B : Value;
通过读取指定的列名,你可以得到,列名在数组中对应的索引值。
对于存放数据的列就不加#,或者换成其他的标识符,同样把读取的数据行拆分存放到数组中,然后这样就很方便的找到列名所对应的列的数据了。
如果是读取行,那也很方便。
建议别使用,String col [] = row.split(":");来拆分数组,最好使用StringBuffer,或char数据来进行拆分操作。
F. Java语言设计,将一个二维数组的行和列元素互换,存到另一个二维数组中
import java.util.Arrays;
public class A{
public static void main(String[] args){
int[][] array1 = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
int row = array1.length;
int column = array1[0].length;
int[][] array2 = new int[row][column];
for(int i=0; i<row; i++) {
for(int j=0;j<column;j++){
array2[i][j] = array1[j][i];
}
}
for(int i=0;i<row;i++){
System.out.println(Arrays.toString(array2[i]));
}
}
}