How to save a two-dimensional arrangement in a one-dimensional

1

I want to save a two-dimensional array in a one-dimensional array that will have an accumulation of data, in days and careers, so I want to save this in a one-dimensional one which will be the sum of the times that they chose a career in the total of days, this arrangement for example, you will have a 6 times race in a total of 5 days, then that is what I want to save in the one-dimensional arrangement.

import java.io.*;
class Carrera{
   public static void main(String arg[])
   {
       int acum[][]=new int[5][6];
       String carrera[]=new String[6];
       int total=Carrera.calcularTotal(acum);
       carrera=Carrera.leerCarrera();
      acum=Carrera.leerDia(carrera); 
      int mayor=Carrera.numeroMayor(carrera,acum); 

       Carrera.imprimirResultado(carrera,total,mayor); 
   }
   public static String[] leerCarrera()
   {
      String carrera[]=new String[6];
         carrera[0]="IMA";
         carrera[1]="IIS";
         carrera[2]="ITC";
         carrera[3]="ITR";
         carrera[4]="IMT";
         carrera[5]="IDA";

         return carrera;
   }


   public static int[][] leerDia(String carrera[])
   {

      int acum[][]=new int[5][6];
      byte deci=0;
      for(int i=0;i<5;i++)
      {
         do{
         System.out.println(" Para dia "+(i+1)+ " Ingrese 6 para "+carrera[5]+" 5 para "+carrera[4]+" 4 para "+carrera[3]+" 3 para "+carrera[2]+" 2 para "+carrera[1]+" 1 para "+carrera[0]+" y 0 cuando no quiera agregar mas carreras");
                  deci=Lectura.readByte();
                  switch(deci)
         {
            case 1:
            acum[i][0]+=1;
            break;
            case 2:
            acum[i][1]+=1;
            break;
            case 3:
            acum[i][2]+=1;
            break;
           case 4:
           acum[i][3]+=1;
           break;
           case 5:
           acum[i][4]+=1;
           break;
           case 6:
           acum[i][5]+=1;
         }
         }while (deci!=0);
      }
      return acum;
   }

   public static int calcularTotal(int acum[][])
   {  
      int total=0;

      for(int i=0;i<5;i++){
      for(int j=0;j<6;j++)
                  total+=acum[i][j];}


      return total;
    }

    public static int numeroMayor (String carrera[], int acum[][])
    {
      int mayor=0;
      for(int i=0;i<6;i++)
      for(int j=0;j<5;j++)
      {
         int acumCarrera[]=new int[6];
             acumCarrera[i]=acumCarrera[i]+acum[j][i];} 

         for(int k=1;k<6;k++)
         {int comparar=acumCarrera[0];
          if (comparar<acumCarrera[k])
               {comparar=acumCarrera[k];
                mayor=k;}
         }
       }
       return mayor;
     }



    public static void imprimirResultado(String carrera[],int total, int mayor)
    {
      System.out.println("La carrera con mayor demanda es "+carrera[mayor]);
      System.out.println("El total de alumnos es de: "+total);
    }

}
    
asked by Angel G Cadena Hernandez 05.05.2016 в 04:38
source

1 answer

2

The best option would be to use a Collection object, in particular any that inherits from List , such as ArrayList , but when you ask only array this would be my applied method to obtain it.

Object[] getUnidimensionalArrayFromMultidimensional(Object[][] matrix, int row) {
     Object[] vector = new Object[matrix.length];
     for(int i = 0; i < vector.length; i++) {
         vector[i] = matrix[i][row];
     }
     return vector;
}
    
answered by 05.05.2016 в 10:14