【问题标题】:Matrix Swapping Rows- Java矩阵交换行 - Java
【发布时间】:2014-04-13 02:06:36
【问题描述】:

我需要编写一个代码来加载一个方阵,然后将第一行与第一列中元素最大的行交换,然后打印交换的数组。

我可以获得第一列的最大元素,但我不知道如何进行交换。

import java.io.*;
import java.util.*;
import static java.lang.System.out;

class Matriz4_Metodos{

    static String cadena;
    static InputStreamReader entrada = new InputStreamReader(System.in);
    static BufferedReader recibedatos = new BufferedReader(entrada);

    public static void main (String[] args) throws java.io.IOException {
    int n,mayor=0;

    n=LeerNumero("Ingresa el numero de los renglones y columnas:");

    int Matriz[][] = new int [n][n];

    Matriz=CargarMatriz(Matriz);

    ImprimirMatriz(Matriz);

    out.println("\nEl mayor de la primera columna es:"+EncontrarMayor(Matriz,mayor));

    }

    public static int LeerNumero (String msgtxt) throws java.io.IOException{

        do{
        int xnm;
        out.print(msgtxt);
        cadena=recibedatos.readLine();
        try{
        xnm=Integer.parseInt(cadena);
            if (xnm<0){
             out.println("Solo positivos");
             continue;
            }
            return xnm;
        }   
            catch (NumberFormatException e){
                out.println("Numero de dato incorrecto");
            }
        }while(true);

    }

    public static int[][] CargarMatriz (int xMatriz[][]) throws java.io.IOException{
    Random Aleatorio =new Random();
    int nal;
        for(int i=0; i<xMatriz.length; i++){
            for(int j=0; j<xMatriz[0].length; j++){
                nal=Aleatorio.nextInt(10);
                xMatriz[i][j]=nal;
            }

        }

        return xMatriz;
    }

    public static void ImprimirMatriz (int xMatriz[][]){
        out.println("\n");
        for(int i=0; i<xMatriz.length; i++){
            for(int j=0; j<xMatriz[0].length; j++){
                out.print(xMatriz[i][j]+" ");
            }
            out.println("\n");
        }
    }

    public static int EncontrarMayor (int xMatriz[][],int xmayor) throws java.io.IOException {
        xmayor=0;

        for(int i=0; i<xMatriz.length; i++){
            for(int j=0; j<xMatriz[0].length; j++){
                if(j==0){
                    if(xMatriz[i][j]>xmayor){
                    xmayor=xMatriz[i][j];
                    }
                }
            }
        }
        return xmayor;
    }

}

【问题讨论】:

  • 请展示你为解决这个问题做了什么。你卡在哪里了?
  • 抱歉忘记密码
  • 如果你要问一个新问题,你应该关闭你的previous duplicate question\。

标签: java arrays matrix


【解决方案1】:

在您之前的问题Matrix Swapping Columns - Java 中找到答案以交换列。

这里是代码。 如需更多说明,请阅读内联 cmets。

    int[][] arr = new int[][] { { 1, 4, 5, 4 }, { 7, 6, 4, 2 }, { 1, 1, 2, 3 }, { 1, 2, 2, 5 } };

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            System.out.print(arr[i][j] + "\t");
        }
        System.out.println();
    }

    // logic begins here
    int rowWithHighestValueInFirstColumn = 1;
    for (int i = 0; i < arr[rowWithHighestValueInFirstColumn].length; i++) {
        // store the value of highest row
        int temp = arr[rowWithHighestValueInFirstColumn][i];
        // swap the value of highest row with first row
        arr[rowWithHighestValueInFirstColumn][i] = arr[0][i];
        // set the value of first row that is stored in temp
        arr[0][i] = temp;
    }
    // logic ends here

    System.out.println("After swapping");
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            System.out.print(arr[i][j] + "\t");
        }
        System.out.println();
    }

输出:(第二行与第一行交换)

    1   4   5   4   
    7   6   4   2   
    1   1   2   3   
    1   2   2   5   
    After swapping
    7   6   4   2   
    1   4   5   4   
    1   1   2   3   
    1   2   2   5

【讨论】:

    【解决方案2】:

    由于矩阵是数组的数组,一旦找到具有最大数字的行的索引,您就可以简单地交换行。更改您的EncontrarMayor 方法以返回行的index,而不是最大的元素,其余的很简单。以下是您的代码的相关更改。 (除了改变EncontrarMayor方法的逻辑,我重命名它以符合标准Java命名约定并删除无用的第二个参数):

    public static void main(String[] args) {
        ...
        int renglonMayor = encontrarMayor(matriz);
        out.println("\nEl mayor de la primera columna es:" + matriz[renglonMayor][0]);
        int[] renglon = matriz[0];
        matriz[0] = matriz[renglonMayor];
        matriz[renglonMayor] = renglon;
        ... // print the matrix
    }
    
    public static int encontrarMayor (int xMatriz[][]) throws java.io.IOException {
        int renglonMayor = 0;
        int numeroMayor = xMatrix[0][0];
    
        for(int i=1; i<xMatriz.length; i++){
            if(xMatriz[i][0]>numeroMayor){
                numeroMayor = xMatrix[i][0];
                renglonMayor = i;
            }
        }
        return renglonMayor;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2019-03-24
      • 1970-01-01
      • 2021-11-09
      • 2022-01-25
      • 1970-01-01
      • 2012-12-26
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多