Solved

# How can I find the smallest number in an arraylist?

Posted on 2009-04-25
1,202 Views
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)
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)
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
Question by:Gazcorp

LVL 11

Expert Comment

use Sort() method like Arraylist.Sort()
0

Author Comment

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

LVL 85

Expert Comment

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

Author Comment

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

LVL 85

Accepted Solution

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

Author Comment

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

Author Comment

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

LVL 85

Expert Comment

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

Author Comment

Okay, that sounds great :)
0

Author Comment

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
``````
0

## Featured Post

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…