矩阵交换Rows-的Java [英] Matrix Swapping Rows- Java

查看:111
本文介绍了矩阵交换Rows-的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手在阵列。

我需要编写一个code键加载一个方阵,然后交换的第一行与在所述第一列中的最大的元素的行,然后打印阵列交换。

I need to write a code to load a square matrix, then swap the first row with the row with the largest element in the first column, then print the array exchanged.

我能得到的第一列的最大元素,但我不如何做交流。

I can get the largest element of the first column but i dont how to do the exchange.

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问题找到答案<一个href=\"http://stackoverflow.com/questions/23038191/matrix-swapping-columns-java?answertab=votes#tab-top\">Matrix交换列 - Java的来交换列

Find the answer at your previous question Matrix Swapping Columns - Java to swap the columns.

下面是code。 有关详细说明,请阅读在线评论。

    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();
    }

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

output: (second row is swapped with first)

    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

这篇关于矩阵交换Rows-的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆