我用数组填充数组列表。每行都是具有属性的项目。现在我想通过一个属性对项目进行排序,并将它们“打印”到textview。
ArrayList<String[]> arrayList = new ArrayList<String[]>();
final String[] rowToArray = new String[7];
rowToArray[0] = itemName;
rowToArray[1] = itemProperties1;
rowToArray[2] = itemProperties2;
rowToArray[3] = itemProperties3;
rowToArray[4] = itemProperties4;
rowToArray[5] = itemProperties5;
rowToArray[6] = itemProperties6;
arrayList.add(rowToArray);
你能帮我按属性对它进行排序,然后告诉我如何用属性逐个打印项目。
提前谢谢。
修改
已解决ppeterka66
我只需要添加他的代码并调用 Collections.sort(arrayList,new StringArrayComparator(column)); 其中列是必需列的排序。
int i=0;
final int column=2;
Collections.sort(arrayList,new StringArrayComparator(column));
for(String[] line :arrayList)
{
Log.d(Integer.toString(i),line[column].toString());
}
答案 0 :(得分:1)
Collections.sort
例如
class User {
String name;
String age;
public User(String name, String age) {
this.name = name;
this.age = age;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.Comparator;
public class ComparatorUser implements Comparator {
public int compare(Object arg0, Object arg1) {
User user0 = (User) arg0;
User user1 = (User) arg1;
int flag = user0.getAge().compareTo(user1.getAge());
if (flag == 0) {
return user0.getName().compareTo(user1.getName());
} else {
return flag;
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortTest {
public static void main(String[] args) {
List userlist = new ArrayList();
userlist.add(new User("dd", "4"));
userlist.add(new User("aa", "1"));
userlist.add(new User("ee", "5"));
userlist.add(new User("bb", "2"));
userlist.add(new User("ff", "5"));
userlist.add(new User("cc", "3"));
userlist.add(new User("gg", "6"));
ComparatorUser comparator = new ComparatorUser();
Collections.sort(userlist, comparator);
for (int i = 0; i < userlist.size(); i++) {
User user_temp = (User) userlist.get(i);
System.out.println(user_temp.getAge() + "," + user_temp.getName());
}
}
}
答案 1 :(得分:1)
您可以创建一个可重用的String [] Comparator,您可以指定要比较数组的索引:
public class StringArrayComparator implements Comparator<String[]> {
//we store the index to compare the arrays by in this instance variable
private final int stringIndexToCompare;
//constructor accepting the value for the index to check
public StringArrayComparator(int whichString) {
stringIndexToCompare=whichString;
}
@Override
public int compare(String[] o1, String[] o2) {
//checking if any of the arrays is null
if(o1==null) { return o2==null?0:1; } //if o1 is null, o2 determines the resuult
else if(o2==null) { return -1; } //this only gets evaluated if o1 is not null
//get the strings, by checking if the arrays are long enough
String first = o1.length>stringIndexToCompare?o1[stringIndexToCompare]:null;
String second= o2.length>stringIndexToCompare?o2[stringIndexToCompare]:null;
//null checking the strings themselves -- basically same as above
if(first==null) { return second==null?0:1; }
else if(second==null) { return -1; }
//if both non-null, compare them.
return first.compareTo(second);
}
}
要在您的列表中使用:
Collections.sort(myList,new StringArrayComparator(3));
注意:3指定要比较的数组的索引。
您没有指定打印字符串外观的预期输出,但只是打印列表,您可以使用此oneliner:
System.out.println(Arrays.deepToString(a.toArray()));
修改强>
我希望看到类似Log.d(“行号”,列[0] +“,”+列1 +“,”+列[2] + ...); < / p>
嘿,看起来几乎没问题......基本上你只需要把它放到一个循环中:这会逐行打印出来:
int lineNo=0;
for(String[] line :myList) {
StringBuilder sb = new StringBuilder();
sb.append(++i); //line number, incrementing too
//iterating through the elements of the array
for(int col=0;col<line.lenght;col++) {
sb.append(",");
if(line[col]!=null) { //check for null....
sb.append(line[col]);
}
}
Log.d(sb.toString()); //append the value from the builder to the log.
}
将它放在一个大字符串中:
int lineNo=0;
StringBuilder sb = new StringBuilder(); //create it here
for(String[] line :myList) {
sb.append(++i); //line number, incrementing too
//iterating through the elements of the array
for(int col=0;col<line.lenght;col++) {
sb.append(",");
if(line[col]!=null) { //check for null....
sb.append(line[col]);
}
}
sb.append("\n"); //append line break
}
Log.d(sb.toString()); //append the value from the builder to the log.
或者,为此目的使用String.format()可能会更好(虽然更慢),这样可以提供更好的格式:
//assembly format string
//if no line number was needed: String format = "";
String format = "%d"; //line number, %d means integer
for(int i=0;i<7;i++) {
format+=",%20s"; //%20s means left aligned, 20 wide string
}
format += "\n"; //line break;
int lineNumber=0;
for(String[] line:myArray) {
//if you didn't need the line number, it would be so easy here
//String.format(format,line); //one line, but this doesn't have the line number yet...
//with line numbers:
int iamLazyNow = 0;
String formatted = String.format(format,++lineNumber,
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++]); //practically one line, but ugly
//you can append formatted to a StringBuilder, or print it here...
}