Skip to content

Instantly share code, notes, and snippets.

@SpinnerZ
Created September 7, 2024 13:10
Show Gist options
  • Select an option

  • Save SpinnerZ/4473f8803c4f9bb57833b12732c6a694 to your computer and use it in GitHub Desktop.

Select an option

Save SpinnerZ/4473f8803c4f9bb57833b12732c6a694 to your computer and use it in GitHub Desktop.
Vetores Multidimensionais em Java
package org.example;
public class Bidimensional {
public static void main(String[] args) {
String[][] matrix;
// int[][] numberMatrix = {
// {1, 2, 3}, // i==0
// {4, 5, 6}, // i==1
// {7, 8, 9} // i==2
// };
int[][] numberMatrix = new int[3][3];
int values = 0;
for (int i = 0; i < numberMatrix.length; i++) {
for (int j = 0; j < numberMatrix[i].length; j++) {
numberMatrix[i][j] = ++values;
}
}
for (int i = 0; i < numberMatrix.length; i++) {
for (int j = 0; j < numberMatrix[i].length; j++) {
System.out.println(numberMatrix[i][j]);
}
}
// matrix = new String[3][2];
matrix =
new String[][] {
// j==0 j==1 j==2
{"lili", "08111", "Sol"}, // i==0
{"lala", "08122", "Lua"}, // i==1
{"maya", "008133", "Estrela"} // i==2
};
System.out.println("\nSomente o lala (linha 1, coluna 0): " + matrix[1][0]);
System.out.println("Somente a linha 1: " + matrix[1][0] + " " + matrix[1][1]);
System.out.println("Linha e Coluna");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}
System.out.println("\nLinha a linha:");
for (int i = 0; i < matrix.length; i++) {
System.out.println();
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j]);
if (j < matrix[i].length - 1) System.out.print("_");
}
}
/*Programa assume que cada linha do vetor multidimensional (matriz) contém a mesma
quantidade de colunas*/
System.out.println("\n\nCada coluna por vez:\n");
int line = 0;
int column = 0;
while (column < matrix[line].length) {
System.out.printf("Coluna %d:\n", column);
for (; line < matrix.length; line++) System.out.println(matrix[line][column]);
line = 0;
column++;
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment