Java 如何对无序链表进行排序
您好,我需要使用选择排序对无序的对象链表进行排序。但是我需要将无序列表转换为数组来使用排序,我不知道如何设置它。之后,我需要将数组添加回无序链表中。这是我的密码Java 如何对无序链表进行排序,java,arrays,Java,Arrays,您好,我需要使用选择排序对无序的对象链表进行排序。但是我需要将无序列表转换为数组来使用排序,我不知道如何设置它。之后,我需要将数组添加回无序链表中。这是我的密码 LinkedUnorderedList <games> c1 = new LinkedUnorderedList <>(); // stack for the code 3 LinkedListStack <games> transactions = new LinkedListStack <g
LinkedUnorderedList <games> c1 = new LinkedUnorderedList <>();
// stack for the code 3
LinkedListStack <games> transactions = new LinkedListStack <games> ();
//Stack for the copy
ArrayStack <games> copyA = new ArrayStack <games> ();
Scanner fileScan = new Scanner(new File("input.txt"));
int code;
String name;
String rt;
String dev;
int year;
games obj;
games tobj;
while(fileScan.hasNext())
{
code = fileScan.nextInt();
if(code == 3)
{
name = fileScan.next();
dev = fileScan.next();
year = fileScan.nextInt();
rt = fileScan.next();
tobj = new games(name,dev);
transactions.push(tobj);
}
else
{
name = fileScan.next();
dev = fileScan.next();
//System.out.print("hello");
year = fileScan.nextInt();
rt = fileScan.next();
obj = new games(name,dev,year,rt);
c1.addToRear(obj);
}
}
System.out.print(c1.toString());
Iterator <games> cpy = c1.iterator();
games element;
while(cpy.hasNext())
{
element = cpy.next();
copyA.push(element);
}
cpy = c1.iterator();
while(cpy.hasNext())
{
info += cpy.next();
}
String ar [] = new String [c1.count];
for(int index = 0; index < c1.count;index++)
{
ar[index] = c1[index];
}
LinkedOnOrderedList c1=新的LinkedOnOrderedList();
//代码3的堆栈
LinkedListStack事务=新LinkedListStack();
//为副本堆叠
ArrayStack copyA=新的ArrayStack();
Scanner fileScan=new Scanner(新文件(“input.txt”);
int代码;
字符串名;
字符串rt;
字符串开发;
国际年;
奥林匹克运动会;
托布吉运动会;
while(fileScan.hasNext())
{
code=fileScan.nextInt();
如果(代码==3)
{
name=fileScan.next();
dev=fileScan.next();
年份=fileScan.nextInt();
rt=fileScan.next();
tobj=新游戏(名称、开发);
交易推送(tobj);
}
其他的
{
name=fileScan.next();
dev=fileScan.next();
//System.out.print(“hello”);
年份=fileScan.nextInt();
rt=fileScan.next();
obj=新游戏(名称、开发、年份、rt);
c1.增补表(obj);
}
}
System.out.print(c1.toString());
迭代器cpy=c1.Iterator();
游戏元素;
while(cpy.hasNext())
{
element=cpy.next();
copyA.push(元素);
}
cpy=c1.iterator();
while(cpy.hasNext())
{
info+=cpy.next();
}
字符串ar[]=新字符串[c1.计数];
for(int index=0;index
您的问题实际上只是询问如何在数组和列表之间转换,而不是如何对它们进行排序。下面是一些指向Java标准库的指针,这些指针与将列表转换为数组以及将列表转换为数组有关
Java集合框架中的所有集合(列表和其他集合的所有形式)都有一个将集合转换为数组的方法
框架的另一个有用部分是类。它有一个方法,可以接受集合
和数组,并为您将数组中的元素添加到集合中
在使用数组时,您需要熟悉handy类。它提供的一个方便方法是返回一个包含所提供数组中所有元素的实现。这可能很有用,例如,和所有集合都有的集合结合使用。对于基元类型:
Collections.sort(myPrimitiveList);
Collections.reverse(myPrimitiveList);
Arrays.sort(myPrimitiveArray);
Arrays.sort(myPrimitiveArray, Collections.reverseOrder());
Collections.sort(myListOfObject, new Comparator<myObject>() {
@Override
public int compare(myObject obj1, myObject obj2) {
int cmp = obj1.compareTo(obj2);
if (cmp > 0) {
return obj1;
} else {
return obj2;
}
}
});
对于其他对象:
Collections.sort(myPrimitiveList);
Collections.reverse(myPrimitiveList);
Arrays.sort(myPrimitiveArray);
Arrays.sort(myPrimitiveArray, Collections.reverseOrder());
Collections.sort(myListOfObject, new Comparator<myObject>() {
@Override
public int compare(myObject obj1, myObject obj2) {
int cmp = obj1.compareTo(obj2);
if (cmp > 0) {
return obj1;
} else {
return obj2;
}
}
});
Collections.sort(myListOfObject,新的Comparator(){
@凌驾
公共整数比较(myObject对象J1、myObject对象J2){
int cmp=obj1。与(obj2)相比;
如果(cmp>0){
返回obj1;
}否则{
返回obj2;
}
}
});
指定一种语言,人们用他们熟悉的语言寻找问题时,这种语言可以帮助您使用此数组
标记非常通用,不太可能找到