使用排序的的compareTo字符串数组的ArrayList()方法 [英] Sorting an ArrayList of String arrays using compareTo() method
问题描述
我是新来的Java。
我想用排序字符串数组的ArrayList String.compareTo()
。
我编了code,使输出是:
因果关系,就是一个,关系
这是也,叫,因果关系
这是约一个,原因,以及它的,影响
块引用>现在我要排序的code(字典序),因此输出是:
因果关系,一个是,关系
同时,它也被称为,因果关系,是
据,一个约,影响和,原因是,其
块引用>不过,我产生了一些疯狂输出。
我的code如下。
任何帮助将是AP preciated。
我对这个可能很简单,几个小时的问题工作,我准备摧毁我的电脑。谢谢
公共类Wk5Q5 { 无效过程1(){ 串S =因果关系是有关系的;
字符串s2 =它也被称为因果关系;
串S =这是关于一个原因及其影响;
ArrayList的<的String []>名单=新的ArrayList<的String []>();
的String [] = ARR1 s1.split();
list.add(ARR1);
的String [] = ARR2 s2.split();
list.add(ARR2);
的String [] = ARR3 s3.split();
list.add(ARR3); / **
* previously排序字符串数组,这样的数组列表
*每个字是用逗号分隔
* /
的for(int i = 0; I<则为list.size();我++){
对于(INT J = 0; J< list.get(I)。长度; J ++){
串T = list.get(一)[J]。 如果(J&0){
T =,+ T;
}
System.out.print(T);
//System.out.println(list.get(i)[j]); }
的System.out.println();
} / **
*我试图在每个列表中的每个字符串排序
* /
对于(INT Z = 0; z,其中,则为list.size(); Z ++){
的for(int i = 0; I< list.get(Z)。长度;我++){
字符串x = list.get(Z)[我]
对于(INT J = I + 1; J< list.get(Z)。长度; J ++){
字符串Y = list.get(Z)[J]。
如果(y.compareTo(X)小于0){
字符串TEMP = list.get(Z)[我]
X = list.get(Z)[J]。
Y =温度;
}
System.out.print(x)的;
} }
}
}
解决方案与实施 <问题EM>选择排序的算法是,你不要修改被排序的名单。当你换
X
和是
,在列表中的相应位置的元素留在自己的老地方。如果您停止使用
X
和是
和list.get代替其使用(Z)[我]
和list.get(Z)[J]
,你的排序算法会产生不同的结果。更妙的是,如果作业允许您使用标准库,看看在<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28java.lang.Object%5B%5D%29\"相对=nofollow>内置Java中数组排序方式。I am new to java.
I'm trying to sort an ArrayList of String arrays using
String.compareTo()
.I have compiled the code so that the output is:
Causality,is,a,relationship
It,is,also,called,causation
It,is,about,a,cause,and,its,affect
Now I want to sort that code (lexicographically) so the output is:
Causality,a,is,relationship
It,also,called,causation,is
It,a,about,affect,and,cause,is,its
However I am generating some crazy output.
My code is below.
Any help would be appreciated.
I have worked on this probably very simple problem for hours and I am ready to destroy my computer. Thanks
public class Wk5Q5 { void process1 () { String s1 = "Causality is a relationship"; String s2 = "It is also called causation"; String s3 = "It is about a cause and its affect"; ArrayList<String[]> list = new ArrayList<String[]>(); String[] arr1 = s1.split(" "); list.add(arr1); String[] arr2 = s2.split(" "); list.add(arr2); String[] arr3 = s3.split(" "); list.add(arr3); /** * previously sorted the arraylist of string arrays so that * each word is separated by commas */ for(int i = 0; i < list.size(); i++){ for (int j = 0; j < list.get(i).length; j++){ String t = list.get(i)[j]; if (j > 0){ t = ", " + t; } System.out.print(t); //System.out.println(list.get(i)[j]); } System.out.println(); } /** * my attempt at sorting each string in each list */ for(int z = 0; z < list.size(); z++){ for(int i = 0; i < list.get(z).length; i++){ String x = list.get(z)[i]; for (int j = i+1; j < list.get(z).length; j++){ String y = list.get(z)[j]; if(y.compareTo(x) < 0) { String temp = list.get(z)[i]; x = list.get(z)[j]; y = temp; } System.out.print(x); } } } }
解决方案The problem with your implementation of the selection sort algorithm is that you do not modify the list being sorted. When you swap
x
andy
, the elements in the corresponding positions of the list remain in their old places.If you stop using
x
andy
and replace their use withlist.get(z)[i]
andlist.get(z)[j]
, your sorting algorithm would produce different results. Better yet, if the homework allows you to use standard library, take a look at a built-in way of sorting arrays in Java.这篇关于使用排序的的compareTo字符串数组的ArrayList()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!