{
static int i
static void sui()
{
System.out.println("\n 排序前:")
float [] a=new float[30]
for(i=0i<30i++)
{
a[i]=(float)(Math.random()*20)//获得30个随机数
System.out.print(" "+a[i]+" ")
if((i+1)%3==0)
System.out.println()
}
pai(a)
}
static void pai(float a[])
{
System.out.println("\n 排序后:")
for(i=0i<29i++)//冒泡法排序
{
for(int j=0j<29-ij++)
{
if(a[j]>a[j+1])
{
float t=a[j]
a[j]=a[j+1]
a[j+1]=t
}
}
}
for(i=0i<30i++)
{
System.out.print(" "+a[i]+" ")
if((i+1)%3==0)
System.out.println()
}
suip(a)//把数组传给下个函数
}
static void suip(float a[]) //把函数打乱,
{
int k=1
int l=0
int d
int [] b=new int[30]
float [] c=new float[30]
c[0]=a[0]
b[0]=(int)(Math.random()*30)
while(k<30)
{
d=(int)(Math.random()*30) //随机一个30内整数给d
for(l=0l<kl++)
{
if(b[l]==d) //d的数和数组里的数比较,若有重复,则退出for循环,返还上面重新给d赋值
break
if(l==k-1) //把d里的数写进c数组
{
b[k]=d
c[d]=a[k]
k++
}
}
}
System.out.println("\n打乱后:")
for(i=0i<30i++)
{
d=b[i] //数组的值给c数组的下标,把a数组的值给c数组
c[d]=a[i]
System.out.print(" "+c[i]+" ")
if((i+1)%3==0)
System.out.println()
}
}
public static void main(String[] args)
{
sui()
}
}
数据结构中的吧,没加权的比如a到b是一条线,加权就是这条线段ab有一个额外的附加含义,比如他长10,或者经过他需要时间为14,等等。加权就是使得一个没意义的东西变得可以权衡了,比如ab长度为3,cd长度为4,那么ab和cd都是加权的了,他们的权值就分别是3和4,他们两者也就可以权衡了,也就在某个情况下有个比较的参考了。public enum SortBy {Number,
Weights1,
Weights2
} import java.util.Comparator
public class MyComparator implements Comparator<Student>{
private SortBy sortBy
public MyComparator (SortBy sortBy){
this.sortBy = sortBy
}
@Override
public int compare(Student s1, Student s2) {
switch (this.sortBy) {
case Weights1:
return s1.getWeights1() - s2.getWeights1()
case Weights2:
return s1.getWeights2() - s2.getWeights2()
default:
return s1.getNumber() - s2.getNumber()
}
}
} import java.util.Collections
import java.util.Comparator
import java.util.LinkedList
import java.util.List
public class Student {
private int number
private int weights1
private int weights2
public Student(int number, int weights1, int weights2) {
this.number = number
this.weights1 = weights1
this.weights2 = weights2
}
public int getNumber() {
return this.number
}
public int getWeights1() {
return this.weights1
}
public int getWeights2() {
return this.weights2
}
public static void main(String[] args){
final Student s1 = new Student(1, 20, 300)
final Student s2 = new Student(2, 10, 100)
final Student s3 = new Student(3, 30, 200)
final List<Student> array = new LinkedList<Student>()
array.add(s1)
array.add(s2)
array.add(s3)
final Comparator<Student> numberComparator = new MyComparator(SortBy.Number)
Collections.sort(array, numberComparator)
System.out.println("sort by number")
System.out.print("Number: ")
for(Student s: array){
System.out.print(s.getNumber() + " ")
}
System.out.println("\n\n")
final Comparator<Student> weights1Comparator1 = new MyComparator(SortBy.Weights1)
System.out.println("sort by weights1")
System.out.print("Number: ")
Collections.sort(array, weights1Comparator1)
for(Student s: array){
System.out.print(s.getNumber() + " ")
}
System.out.println("\n\n")
final Comparator<Student> weights2Comparator = new MyComparator(SortBy.Weights2)
System.out.println("sort by weights2")
System.out.print("Number: ")
Collections.sort(array, weights2Comparator)
for(Student s: array){
System.out.print(s.getNumber() + " ")
}
System.out.println("\n\n")
}
} sort by number
Number: 1 2 3
sort by weights1
Number: 2 1 3
sort by weights2
Number: 2 3 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)