domingo, 24 de abril de 2011

Como agregar y Ordenar elementos de un Vector


package metodoburbuja;
import java.util.Scanner;
/**
 *
 * @author Lidia
 */
public class BurbujaMejorada {


    public static void main(String[] args) {


        int v[] = new int[5];
        int t;
        
        Scanner teclado = new Scanner(System.in);
        
        for(t = 0 ; t < v.length ; t++){
            System.out.print("Ingrese un Numero:");
            v[t] = teclado.nextInt();
        }
        
            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;
                    }
                    
                }
            }
            for(int i = 0 ; i < v.length; i++)
                System.out.print("  "+v[i]);
                System.out.println();
        }
     }
  





package agregardatosvector;
import java.util.Arrays;
import java.util.Vector;
/**
 *
 * @author Lidia
 */
public class AgregarVector {


    public static void main(String[] args) {
        int v[] = new int[6];
        Vector v2 = new Vector(5);
        
        v[0] = 250;
        v[1] = 369;
        v[2] = 123;
        v[3] = 876;
        v[4] = 900;
        v[5] = 432;
        
        v2.add(300);
        v2.add(200);
        v2.add(150);
        v2.add(344);
        v2.add(104);
        v2.add(450);
        
        System.out.print("Elemtos Agregados con la Clase Vector\n");
        System.out.println(v2);
        
        System.out.print("Elemtos Agregados de acuerdo a su posicion\n");
         Arrays.sort(v);
        for(int i = 0 ; i < v.length ; i ++)
        System.out.print("["+v[i]+"]");
        }
}


No hay comentarios:

Publicar un comentario