Lo que no me debo olvidar de vectores


LO QUE NO DEBO OLVIDAR DE LOS VECTORES
             
             * Inicializacion
             * Ingresar Datos
             * Mostrar Datos
             * Otros Algoritmos
             
                            --> ORDENACION
                            --> BUSQUEDA


1) INICIALIZACION.-
                                 
                             int v[] = new int[n];
                             Object O[] = new Object[n];
                             int v1[] = {2, 9, 7, 8};
                             Vector v2 = new Vector(5);
                             //Para utilizar la clase Vector debemos hacer un import
                             //import java.Util.Vector;
                             int v3; // Solo es una referencia


2) INGRESAR DATOS.-


                            v[0] = 25;
                            v2.add(300);
                            v2.add(200);
                            v2.add(10, 4);


2.1) INGRESAR DATOS POR TECLADO.-


                           BufferedReader val = (new val BufferedReader(new InputStreamReader(System.in)));
                           int num = Integer.ParseInt(val.ReadLine());
                              for(i = 0 ; i < num.length ; i++){
                                   v[i] = Integer.ParseInt(val.ReadLine());
                              }


3) MOSTRAR DATOS.-


                          public Static void Mostrar(int x[]){
                                 for(i = 0 ; i < x.length ; i++){
                                      System.Out.Println("["+ i + "]" + x[i]);
                                 }
                          }
                         //invocacion al metodo main()
                         public static void main(String[] args) {
                            Mostrar(v);
                         }


3.1) MOSTRAR CON LA CLASE VECTOR.-


                        Public Static void Mostrar(Vector x){
                             for(i = 0 ; i < x.SIZE() ; i++){
                                  System.Out.Println("[" + i + "]" + x.get(i));
                             }
                        }


4) ORDENACION.-


                        Public void Ordenar(int v[]){
                            for(i = 0 ; i < v.length ; i++){
                                 for(j = 0 ; j < v.length ; j++){
                                      if(v[i] < v[j]){
                                      int aux = v[i];
                                            v[i] = v[j];
                                            v[j] = aux;
                                      }
                                  }
                             }
                        }


                        // tenemos que hacer el import necesario
                        import java.Util.Arrays;
                        Arrays.Sort(v);
                        Mostrar(v);


5) BUSQUEDA.-


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


                       //hacemos el import correspondiente
                       import java.Util.Arrays;
                       Arrays.BinarySearch(v, elem);                         

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

1 comentarios:

Liliana Arggiro Soruco dijo...

Trabajo corregido, cambiar el color y estilo de fuente para los cod. java

Publicar un comentario