Foros del Web » Programación para mayores de 30 ;) » .NET »

arreglos en vb.net - invertir

Estas en el tema de arreglos en vb.net - invertir en el foro de .NET en Foros del Web. me pueden indicar cual es el algoritmo o el codigo para invertir un arreglo, por favor. este es el que estoy usando For i = ...
  #1 (permalink)  
Antiguo 29/05/2007, 16:58
fanely
Invitado
 
Mensajes: n/a
Puntos:
arreglos en vb.net - invertir

me pueden indicar cual es el algoritmo o el codigo para invertir un arreglo, por favor.
este es el que estoy usando
For i = 1 To 5 Step 1
For j = 5 To 1 Step -1
aux = datos(i)
datos(i) = datos(j)
datos(j) = aux
Next
Me.ListBox2.Items.Add(datos(i))
Next

ingreso: 5,3,7,2,5
y me aparece: 3,3,2,3,5

gracias por sus respuestas
  #2 (permalink)  
Antiguo 29/05/2007, 18:52
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 19 años, 4 meses
Puntos: 7
Re: arreglos en vb.net - invertir

Usa Array.Reverse

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
  #3 (permalink)  
Antiguo 30/05/2007, 15:07
fanely
Invitado
 
Mensajes: n/a
Puntos:
Mensaje Re: arreglos en vb.net - invertir

Hola Alex Concha, gracias por tu pronta respuesta, ya me invierte el vector, pero tengo dudas
Dim datos(5), aux, i, j As Integer
para añadir arreglo, lo hago comenzando de 1 al 5
For i = 1 To 5
datos(i) = Val(InputBox("ing. dato " & i & " : "))
Me.ListBox1.Items.Add(datos(i))
Next
Array.Reverse(datos)
y para mostrar, lo hago de 0 a 4
For i = 0 To 4
Me.ListBox2.Items.Add(datos(i))
Next
por que es esto?
gracias
  #4 (permalink)  
Antiguo 30/05/2007, 17:43
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 19 años, 4 meses
Puntos: 7
Re: arreglos en vb.net - invertir

Es que estás declarando un arreglo de 6 elementos [datos(5)], ten en cuenta que en .NET los arreglos empiezan en 0.

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
  #5 (permalink)  
Antiguo 31/05/2007, 10:32
(Desactivado)
 
Fecha de Ingreso: julio-2006
Mensajes: 273
Antigüedad: 17 años, 9 meses
Puntos: 5
De acuerdo Re: arreglos en vb.net - invertir

aca esta el codigo. talvez te sirva

Public Class Form1
Private i As Integer

Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
If txtNuevo.Text <> Nothing And IsNumeric(txtNuevo.Text) Then
i += 1
Lista.Items.Add(txtNuevo.Text)
txtNuevo.Text = Nothing
Else
MsgBox("Error")
txtNuevo.Text = Nothing
End If
End Sub

Private Sub Burbuja()
Dim x, y, aux As Integer
For x = 0 To i
For y = 0 To i - 1
If Lista.Items(y) > Lista.Items(y + 1) Then
aux = Lista.Items(y)
Lista.Items(y) = Lista.Items(y + 1)
Lista.Items(y + 1) = aux
End If
Next
Next
End Sub

Private Sub burbujadoble()
Dim x, y, aux As Integer
For x = 0 To i
For y = 0 To i - 1
If Lista.Items(y) < Lista.Items(y + 1) Then
aux = Lista.Items(y)
Lista.Items(y) = Lista.Items(y + 1)
Lista.Items(y + 1) = aux
End If
Next
Next
End Sub


Private Sub btnOrdenDes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrdenDes.Click
i = Lista.Items.Count - 1
Burbuja()
End Sub

Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
Me.Close()
End Sub

Private Sub btnOrdenAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrdenAs.Click
i = Lista.Items.Count - 1
burbujadoble()
End Sub
End Class

si no lo entiendes muy bien enviame un mensaje para pasarte por correo la aplicacion.
  #6 (permalink)  
Antiguo 22/06/2007, 01:10
 
Fecha de Ingreso: junio-2007
Mensajes: 9
Antigüedad: 16 años, 10 meses
Puntos: 0
Re: arreglos en vb.net - invertir

Hola esta función invierte una cadena ya sea números o texto

Function invertirCadena(ByVal textoEntrada As String) As String

Dim i As Integer

Dim textoSalida As String

textoSalida = ""

For i = textoEntrada.Length To 0 Step -1

Try
textoSalida += textoEntrada.Substring(i, 1)

'MessageBox.Show(textoEntrada.Substring(i, 1))

Catch ex As Exception

End Try

Next

Return textoSalida

End Function
Aunque quizas debería mirarte Reverse
Un Saludo
  #7 (permalink)  
Antiguo 28/10/2008, 20:43
 
Fecha de Ingreso: octubre-2008
Mensajes: 1
Antigüedad: 15 años, 6 meses
Puntos: 0
Respuesta: Re: arreglos en vb.net - invertir

ese era el codigo que estaba buscando, gracias muchas gracias... pero no entiendo tanto como tu lo del For try ,etc. te volveria a dar gracias si me ayudas orientandome algo mas, yo intentaba hacer eso de invertir usando do while.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:35.