Viruz Blog
¿Quieres reaccionar a este mensaje? Regístrate en el foro con unos pocos clics o inicia sesión para continuar.

Manejo de vector

Ir abajo

Manejo de vector Empty Manejo de vector

Mensaje  Admin (Viruz) Dom Nov 18, 2012 4:17 pm

Hola que tal amigos de Viruz Blog ya es fin de año y será al parecer el último tutorial de este 2011. Como comenzamos alguna vez con este blog con Java terminaremos igual, esperando que este 2012 nos venga con muchas más sorpresas y tiempo para poder abastecer todos los temas.

En esta ocasión tenemos el uso de vectores en Java que son muy importantes en la programación y tengo un claro ejemplo de como poder utilizarlos.

Vector = Conjunto de Variables.
Matriz = Conjunto de Vectores.

Lo que se necesita conocer
  • Suma de matriz (Array)
Imagen
[Tienes que estar registrado y conectado para ver esa imagen]
¿Como funciona el Programa?
  1. Para obtener los elementos que se almacenan en el vector se realiza mediante el método JOptionPane.showInputDialog() y después de un ciclo for() se realiza el llenado del vector.
  2. Para mostrar los datos se utiliza el método JOptionPane.showMessageDialog() y de parámetro se pasa el método getDatos() de la clase Vector.
Código

Clase Main
/**
*
* @author Viruz
*/
public class Main {
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VectorGUI().setVisible(true);
}
});
}
}

Clase VectorGUI

import javax.swing.JOptionPane;
/**
*
* @author Viruz
*/
public class VectorGUI extends javax.swing.JFrame {
int elementos;
private Vector vector; // se instancia un objeto de la clase Vector

public VectorGUI() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Viruz Blog: Vectores");

jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 20)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Manejo de Vectores en Java");

jButton1.setText("Crear Vector");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Introducir Datos");
jButton2.setEnabled(false);
jButton2.setPreferredSize(new java.awt.Dimension(93, 23));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Mostrar Datos");
jButton3.setEnabled(false);
jButton3.setPreferredSize(new java.awt.Dimension(93, 23));
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("Salir");
jButton4.setPreferredSize(new java.awt.Dimension(93, 23));
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.
DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.
DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.
DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(30, 30, 30)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.
PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.
PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.
PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.
PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.
PREFERRED_SIZE)
.addContainerGap(60, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton2.setEnabled(true);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
elementos=Integer.parseInt(JOptionPane.showInputDialog("Introduzca el numero de Elementos"));
String datos[] = new String [elementos];
for (int i=0;i<elementos;i++){ //Este es el ciclo en el que se clasifica el elemento.
datos[i]=JOptionPane.showInputDialog("\n Asigne un Elemento ");
}
vector = new Vector(elementos,datos); //Estoy asignandole valores al objeto arreglo
jButton3.setEnabled(true);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null," "+vector.getdatos());
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null,"Viruz Blog: Gracias");
System.exit(0);
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}


Clase Vector
public class Vector{
private String datos[];

public Vector(int elemento,String datos[]){
this.datos=datos;
}

//Metodo que muestra los datos
public String getdatos(){
String llenar=""; // Variable que acumulara los datos
for(int i=0;i<datos.length;i++){ //ciclo que
llenar=llenar+"\n"+datos[i];
}
return ("Los Datos son:" +llenar);
}
}

Vídeo



Descarga el código
Spoiler:
Autor: Viruz study
Enlace: Manejo de vectores en Java
Admin (Viruz)
Admin (Viruz)
Admin

Mensajes : 148
Fecha de inscripción : 23/09/2012
Edad : 35
Localización : Desktop

https://viruz.foroactivo.mx

Volver arriba Ir abajo

Volver arriba

- Temas similares

 
Permisos de este foro:
No puedes responder a temas en este foro.