domingo, 24 de abril de 2011

RECORDANDO VECTORES Y MATRICES

Miercoles 20 abril 2011
Estructura de datos


VECTORES
Vectores.- unidad de almacenamiento de datos(temporal o permanente).
·         INICIALIZAR.
·         INGRESAR DATOS.
·         MOSTRAR DATOS.
OTROS ALGORITMOS:
ü  BUSQUEDA.
ü  ORDENAMIENTO.

1.- INICIALIZAR UN VECTOR

Int  V[ ] = new int [ n ]; 
Object  O[ ] = new Object[ n ];
Int  V1 [ ] = { 2, 7, 1, 9 };
Vector V2 = new Vector( 5 ); 
Int V3[  ];


2.- INGRESAR DATOS
25
0
0

      


V2.add(300); 
V2.add(200);
V2.add(10, 4);   

300
200
0
0
10


                     
BufferReader Val = (new BufferReader(new Imput SheamReader(System.In)));
// Introducimos valor desde teclado.
Int num = Integer.ParseIn(Val.readLine() ); // Captura por flujo del teclado
For( int i = 0; i < n; i ++){
            V1[ i ] = Integer.ParseIn(Val.readLine());
}


3.- MOSTRAR DATOS

Public static void Mostrar( int V[ ] ){
            For( int i = 0; i < V.length; i ++ ){ // Por propiedad o atributo(length).
                        System.out.println(“ [ ” + i + “ ] =  ” + V[ i ]);
}
}
// Invocacion en el main del metodo Mostrar
public static void main(String[] args) {
Mostrar(V);
}
Con la clase vector

Public static void MostrarV(Vector  X ){
            For( int i = 0; i < X.Size; i ++ ){
                  System.out.println(“ [ ” + i + “ ] =  ” + X.get( i ));
}
4.- OTROS ALGORITMOS 
(ORDENACION )  
.-Forma tradicional.

public static void Ordenar( int V[ ]){
 for( int i = 0; i < V.length; i ++ ){
    for( int j = 0; j < V.length; j ++ ){
     if(V[ i ] < V[ j ]){
      int aux = V[ i ];
       V[ i ] = V[j];
       V[ j ] = aux;
      }else if(V[ i ] > V[ j ]){
       int aux = V[ i ];
       V[ i ] = aux;
       V[ j ] = V[j];}
    }
   }
 }
Public static void Mostrar( int V[ ] ){
            For( int i = 0; i < V.length; i ++ ){  
System.out.println(“ [ ” + i + “ ] =  ” + V[ i ]);  }
}
.- Utilizando metodos de la clase ARRAYS.
Importamos la librería

Import  java.util.Arrrays;

Public static void Mostrar( int V[ ] ){
            For( int i = 0; i < V.length; i ++ ){ // Por propiedad o atributo(length).
                        System.out.println(“ [ ” + i + “ ] =  ” + V[ i ]);
}
}
public static void main(String[] args) {
       int v[]= {2,9,1,3};
        Arrays.sort(v);
        Mostrar(v);
    }
(BUSQUEDA )  

public static void llenar( int V[ ]){
    Scanner S= new Scanner(System.in);
    for( int i = 0; i < V.length; i ++ ){
            V[i]= S.nextInt();
    }
}
public static int buscar(int V[], int elem){
   int pos =-1;
    for( int i = 0; i < V.length; i ++ ){
        if(V[ i ] == elem){
            pos =i;
        }
    }
   return pos;
}
public static void Mostrar( int V[ ] ){
    int i;
    for( i = 0; i < V.length; i ++ ){
     out.println(" [ " + i + " ] =  " + V[i]);
     }
}
public static void main(String[] args) {

      out.println("Longitud de nuestro vector:");
       Scanner N = new Scanner(System.in);
       int n = N.nextInt();
       int v[]= new int[n];

        out.println("Llenamos nuestro vector:");
        llenar(v);

        out.println("buscamos elemento en el vector:");
        Scanner S = new Scanner(System.in);
        int elem = S.nextInt();

      out.println("Mostramos vector desordenado: ");
       Mostrar(v);
       
       out.println("Mostramos vector ordenado: ");
       Arrays.sort(v);
       Mostrar(v);

       out.println("La posicion del elemento encontrado es :"+buscar(v, elem));
       }
}
MATRICES
Matrices.-
·         INICIALIZAR.
·         INGRESAR DATOS.
·         MOSTRAR DATOS.

1.- INICIALIZAR UNA MATRIZ

·         Int  M[ ] [ ]= new int [ n ][ n ];  // El operador new es una asignacion dinamica de memoria.
·         Object  O[ ] [  ]= new Object[ n ][ n ];  // Crea una matriz de objetos de tamaño n*n.
·         Arrays[ ][ ] V2 = new Arrays[5][5];  // Es un método, asi que importamos la librería import java.util.Vecto; o : import java.util.Arrays;
·         Int M3[  ][  ]; // Esto hace una referencia no es una matriz ya que esto no es una instancia.
2.- INGRESAR DATOS

BUFFER .- Almacenamiento temporal de memoria auxiliar.
 M[ 0 ][ 2 ] = 25;  
0
0
25
0
0
0

             



·         BufferReader Val = (new BufferReader(new Imput SheamReader(System.In)));
// Introducimos valor desde teclado.
·         Int num = Integer.ParseIn(Val.readLine() ); // Captura por flujo del teclado
For( int i = 0; i < n; i ++){
For( int j = 0; j < n; j ++){
      V1[ i ] [ j ] = Integer.ParseIn(Val.readLine());
       }
}
3.- MOSTRAR DATOS

public static void Mostrar( int V[ ][ ] ){
    for( int i = 0; i < V.length; i ++ ){
        for( int j = 0; j < V.length; j ++ ){
            System.out.print( V[i][j]+"\t");
        }
        System.out.print("\n");
    }
}
// Invocacion en el main del metodo Mostrar
public static void main(String[] args) {
            int int M[ ][ ]= new int [ n ][ n ];
            System.out.println(“Mostramos elementos de la matriz: ”);
Mostrar(M);
}



V[ 0 ] = 25;

2 comentarios: