Vetor de/com Matrizes [RESOLVIDO]

5 respostas
waldineyrodrigues

Olá pessoal,

Em uma aplicação que estou desenvolvendo, preciso guardar várias matrizes dentro de um vetor.
Basicamente os vetores são do tipo inteiro, string, objetos, etc. Gostaria de saber se é possível criar um vetor que armazene várias matrizes.
Como poderei declarar um vetor assim?

5 Respostas

diego.sas
Vê se é isso!
public static int [][] intArray = new int [5][5];
	public static String [][] stringArray = new String [5][5];	
	public static Object [] vetor = new Object[2];
	
	public static void main(String[] args) {
		vetor[0]= intArray;
		vetor[1] = stringArray;
		
	}
Flw
M

Bom, acho que é isso que você precisa.

// vetor de 10 posiçoes
String[] vetor = new String[10];

//materiz 1
String[][] matriz1 = new String[10][10];
vetor[0] = matriz1;

//materiz 2
Integer[][] matriz2 = new Integer[10][10];
vetor[1] = matriz2;

Boa sorte

diego.sas

Murilo_Ferreira:
Bom, acho que é isso que você precisa.

// vetor de 10 posiçoes
String[] vetor = new String[10];

//materiz 1
String[][] matriz1 = new String[10][10];
vetor[0] = matriz1;

//materiz 2
Integer[][] matriz2 = new Integer[10][10];
vetor[1] = matriz2;

Boa sorte

Boa tarde Murilo_Ferreira, se vc declarar

String vetor

Não compilará o código…
Abraços

M

Oops, desculpa pessoal, mas podemos corrigir facilmente

// vetor de 10 posiçoes
Object[] vetor = new Object[10];

//materiz 1
String[][] matriz1 = new String[10][10];
vetor[0] = matriz1;

//materiz 2
Integer[][] matriz2 = new Integer[10][10];
vetor[1] = matriz2;

diego.sas
obrigado pela observação.

waldineyrodrigues

diego.sas e Murilo_Ferreira... Valeu, deu certinho, com a ajuda de vocês eu consegui resolver o problema no código.
Exemplificando, ficou assim em uma classe de teste...

public class Teste {
    
    public static String [][] minhaMatriz1 = new String [5][5];
    public static String [][] minhaMatriz2 = new String [5][5];    
    public static Object [] vetor = new Object[2];  
      
    public void Teste() {
        minhaMatriz1[0][0]="Testando";
        minhaMatriz1[0][1]="as posições";
        minhaMatriz1[2][0]="da matriz";
        
        vetor[0]= minhaMatriz1;//"Guardando matriz no Vetor"
        
        minhaMatriz2=(String[][]) vetor[0];//"Tirando a matriz de dentro do vetor, e, fazendo a conversão"
        
        System.out.println(minhaMatriz2[0][0]);//Imprimindo o teste
        System.out.println(minhaMatriz2[0][1]);//Imprimindo o teste
        System.out.println(minhaMatriz2[2][0]);//Imprimindo o teste
    }  
}
Criado 23 de novembro de 2011
Ultima resposta 23 de nov. de 2011
Respostas 5
Participantes 3