• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1233
  • Last Modified:

How can I find the smallest number in an arraylist?

I have 3 arraylists. I am working on a program that can plot out a tilepath for a game.

I have an arraylist named "nodes". In it are a bunch of lines in this format: 2561:1245 (not exact numbers). They represent different tiles in the game.

        Dim distanceFromStart As New ArrayList()
        Dim distanceFromEnd As New ArrayList()

        For i As Integer = 0 To nodes.Count() - 1
            Dim x As Integer = ParseLeft(nodes(i), ":")
            Dim y As Integer = ParseRight(nodes(i), ":")
            Dim dist As Integer = distance(endTile, x, y)
            distanceFromEnd.Add(dist)
        Next

        For i As Integer = 0 To nodes.Count() - 1
            Dim x As Integer = ParseLeft(nodes(i), ":")
            Dim y As Integer = ParseRight(nodes(i), ":")
            Dim dist As Integer = distance(startTile, x, y)
            distanceFromStart.Add(dist)
        Next


My distance function finds the distance between the 2 tiles.

I want to find which tile is closest to startTile, but between startTile and endTile.

My tile class just contains two integers, x and y
Thanks!

0
Gazcorp
Asked:
Gazcorp
  • 6
  • 3
1 Solution
 
srikanthreddyn143Commented:
use Sort() method like Arraylist.Sort()
0
 
GazcorpAuthor Commented:
I know that, but then I can't keep track of which Distance goes with which Tile. I was doing it before with the index
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why not make your Class a little more verbose so you can store the calculated information WITHIN each node?

Then you can use LINQ to easily sort the nodes in different ways or find min/max values...all without changing the order of the original list.  (ArrayLists seem an odd choice of storage given that you are using .Net 3.X)

Also, why are you storing numeric data in a delimited string like that?  Your code would run faster if you didn't have to parse it first...

These comments may be way off base, though, since we really can't see the "big picture" of your app.  =\
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
GazcorpAuthor Commented:
What you said made perfect sense :) Mind giving me an example of how to save my data / sort it with linq?

Like, I would load the node list, correct? Then parse each tile into X class. How would I save the list of X that I made?

Sorry if that's confusing.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For me to make any suggestions of any use I would need to see a lot more of your code...  =\

Or at least understand better what you are doing.

You are populating "nodes" with a bunch of coordinates?  x:y
It seems like you should be putting actual REFERENCES to the nodes in to a List and then querying the node for its X,Y values to make the calculations.  Or even passing the the "target" start/end tile into the Tile class so that it can return its distance to that node.

Just seems like the whole thing would be easier to manage and EXTEND if you had more of an Object Oriented approach.

But again, I would need to see ALOT more code to definitely say either way and make good suggestions.  How big is the current project?
0
 
GazcorpAuthor Commented:
Not very big. The tile class is imported from another dll, and it just contains x and y. I can create a new one to contain more things though.

The nodes list is just a text file with x:y over and over again.


    Public Function ParseLeft(ByVal strSource As String, ByVal strEnd As String) As String
        Return Left(strSource, strSource.IndexOf(strEnd))
    End Function
    Public Function ParseRight(ByVal strSource As String, ByVal strStart As String) As String
        Return Right(strSource, strSource.Length - strSource.IndexOf(strStart) - strStart.Length)
    End Function


    Function distance(ByVal startTile As Tile, ByVal endX As Integer, ByVal endY As Integer) As Integer
        Dim dx As Integer = startTile.x - endX
        Dim dy As Integer = startTile.y - endY
        Return Math.Sqrt(dx * dx + dy * dy)

    End Function

    Function distance(ByVal startTile As Tile, ByVal endTile As Tile) As Integer
        Dim dx As Integer = startTile.x - endTile.x
        Dim dy As Integer = startTile.y - endTile.y
        Return Math.Sqrt(dx * dx + dy * dy)
    End Function

What else do you want to see? :D
0
 
GazcorpAuthor Commented:
Okay, I modified my tile class to support some more properties:

Public Class Tile
    Public distFrStart As Integer
    Public distFrEnd As Integer
    Public x As Integer, y As Integer

    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal distFrStart As Integer, ByVal distFrEnd As Integer)
        Me.x = x
        Me.y = y
        Me.distFrEnd = distFrEnd
        Me.distFrStart = distFrStart
    End Sub
End Class


my code:

Dim Tilelist as new ArrayList()
Tilelist.Add(New Tile(x, y, distance(startTile, x, y), distance(endTile, x, y)))

Now, how I can use these to determine which tile is closest to startTile, but between startTile and endTile? I can use Tilelist(i).x, Tilelist(i).y, etc. This is so much easier!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm actually on vacation and about to go to a lake for a bday party...

I'll take a closer look and possibly post some code this evening though.  =)
0
 
GazcorpAuthor Commented:
Okay, that sounds great :)
0
 
GazcorpAuthor Commented:
I am now using the following function and it works GREAT! Thanks for all your help :)


 Public Sub SortObjectCol(ByVal List As ArrayList, ByVal min As Integer, _
  ByVal max As Integer, ByVal propName As String)
        Dim last_swap As Integer
        Dim i As Integer
        Dim j As Integer
        Dim tmp As Object
 
        ' Repeat until we are done.
        Do While min < max
            ' Bubble up.
            last_swap = min - 1
            ' For i = min + 1 To max
            i = min + 1
            Do While i <= max
                ' Find a bubble.
				If CallByName(List(i - 1), propName, CallType.Get) > CallByName(List(i), propName, CallType.Get) Then
                    ' See where to drop the bubble.
                    tmp = List(i - 1)
                    j = i
                    Do
                        List(j - 1) = List(j)
                        j = j + 1
                        If j > max Then Exit Do
                    Loop While CallByName(List(j), propName, CallType.Get) < CallByName(tmp, propName, CallType.Get)
                    List(j - 1) = tmp
                    last_swap = j - 1
                    i = j + 1
                Else
                    i = i + 1
                End If
            Loop
            ' Update max.
            max = last_swap - 1
 
            ' Bubble down.
            last_swap = max + 1
            ' For i = max - 1 To min Step -1
            i = max - 1
            Do While i >= min
                ' Find a bubble.
				If CallByName(List(i + 1), propName, CallType.Get) < CallByName(List(i), propName, CallType.Get) Then
                    ' See where to drop the bubble.
                    tmp = List(i + 1)
                    j = i
                    Do
                        List(j + 1) = List(j)
                        j = j - 1
                        If j < min Then Exit Do
					Loop While CallByName(List(j), propName, CallType.Get) > CallByName(tmp, propName, CallType.Get)
                    List(j + 1) = tmp
                    last_swap = j + 1
                    i = j - 1
                Else
                    i = i - 1
                End If
            Loop
            ' Update min.
            min = last_swap + 1
        Loop
    End Sub

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now