新聞中心
vB.net語言編寫N個數(shù)字數(shù)組排序可自定義數(shù)組長度的
Dim d() As Integer
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比新華網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式新華網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋新華地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim T As Integer
n = Val(InputBox("請輸入n,確定數(shù)組的元素個數(shù)"))
ReDim d(n)
'用隨機函數(shù)生成正整數(shù)數(shù)組
Randomize
For i = 1 To n
d(i) = Int(Rnd * 100 + 1)
Next i
'輸出
For i = 1 To n
Print d(i),
Next i
'從小到大排序
For i = 1 To n - 1
For j = i + 1 To n
If d(i) d(j) Then
T = d(i)
d(i) = d(j)
d(j) = T
End If
Next j
Next i
'輸出
For i = 1 To n
Print d(i),
Next i
VB.NET數(shù)組的排序法?
如果你是從vb6剛過渡上vb。net,建議還是用冒泡排序法,容易理解。
如果你正努力學習vb。net的方法,推薦一個例子如下:
Imports System
Imports System.Collections
Public Class SamplesArray
Public Class myReverserClass
Implements IComparer
' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare
End Class 'myReverserClass
Public Shared Sub Main()
' Creates and initializes a new Array and a new custom comparer.
Dim myArr As [String]() = {"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}
Dim myComparer = New myReverserClass()
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the default comparer.
Array.Sort(myArr, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myArr, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myArr, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myArr() As [String])
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArray
'This code produces the following output.
'
'The Array initially contains the following values:
' [0] : The
' [1] : QUICK
' [2] : BROWN
' [3] : FOX
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the default comparer:
' [0] : The
' [1] : BROWN
' [2] : FOX
' [3] : QUICK
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
' [0] : The
' [1] : QUICK
' [2] : FOX
' [3] : BROWN
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting the entire Array using the default comparer:
' [0] : BROWN
' [1] : dog
' [2] : FOX
' [3] : jumps
' [4] : lazy
' [5] : over
' [6] : QUICK
' [7] : the
' [8] : The
'
'After sorting the entire Array using the reverse case-insensitive comparer:
' [0] : the
' [1] : The
' [2] : QUICK
' [3] : over
' [4] : lazy
' [5] : jumps
' [6] : FOX
' [7] : dog
' [8] : BROWN
VB.NET 控件數(shù)組拖動排序
因為控件按的比較緊密,需要同時判斷數(shù)據(jù)列是否拖動在某個控件上,如果超過控件寬度的一半,則默認把當前拖動的控件放到該控件后面。否則,位置不動
VB.net 數(shù)組怎么按任意元素的順序排序輸出
你直接傳一個數(shù)組進去,而且是一個結(jié)構(gòu)體數(shù)組,array.sort怎么知道根據(jù)結(jié)構(gòu)中的哪一個屬性進行排序?放一個c#的代碼你看看,VB和C#很相似的
class Program
{
static void Main(string[] args)
{
People[] p = new People[3]
{
new People{name="張三"},
new People{name="李四"},
new People{name="張二名"}
};
//重點傳一個實現(xiàn)了IComparer接口的類進去,告訴Array.Sort怎么排序
Array.Sort(p, new PeopleCompare());
foreach (var item in p)
{
Console.WriteLine(item.name);
}
Console.ReadKey();
}
}
//People結(jié)構(gòu)體,換成類一樣的
public struct People
{
public string name { get; set; }
}
//實現(xiàn)了IComparer接口的類
public class PeopleCompare : IComparer
{
public int Compare(object x, object y)
{
People p1 = (People)x ;
People p2 = (People)y;
return p1.name.CompareTo(p2.name);
}
}
網(wǎng)頁名稱:vb.net數(shù)組排序,vb生成隨機數(shù)組排序
轉(zhuǎn)載來源:http://fisionsoft.com.cn/article/hsched.html