2012-11-01 45 views
0

我正在学习Java和排序。Java按唯一索引号排序值

我有一个跟踪重复值的索引号的问题。

例如,我们有桌子,我把所有的数据到ArrayList,就像这样:

ArrayList = {FOO , AA, BOB, AA, BOB} 

Index | Value 
1  | FOO 
2  | AA 
3  | BOB 
4  | AA 
5  | BOB 

现在我想排序的数据:

Index | Value 
2  | AA 
4  | AA 
3  | BOB 
5  | BOB 
1  | FOO 

有什么办法保持唯一的索引和排序数据?

谢谢。

+0

想象中的数据已经在整理状态。如果你想创建下一个元素,你只需要得到一个正确的索引。怎么做? –

回答

1

创建一个类

class DataHelper{ 
    private String name; 
    private int index; 
    // other stuff 
} 

,创造List<DataHelper>谱写Comparator排序DataHelpers

+0

你能举个例子吗?我是JAVA的初学者:(比较的对不起 –

+0

例子吗? –

+0

是...我在做什么是我创建的类datahelper和\t \t \t公共DataHelper(INT指数,字符串名称){ \t \t \t \t this.index =指数; \t \t \t \t this.name =名称; \t \t \t} –

0

您可以使用以下排序的ArrayList或者任何集合的子类。

// unsortstList is an ArrayList 
Collections.sort(unsoredtList); 
0

创建一个将保存索引和文本字符串的对象。像

public class MyThing 
{ 
    public int index; 
    public String text; 
} 

然后,不是创建一个字符串的ArrayList,而是创建这些对象的ArrayList。

我不知道你在用什么来排序它们。如果你正在写自己的排序,那么你可以简单地对每个对象的“文本”成员进行比较,而不是针对字符串对象本身。如果您使用Arrays.sort对其进行排序,那么您需要实现Comparable。即:

public class MyThing implements Comparable<MyThing> 
{ 
    public int index; 
    public String text; 

    public int compareTo(MyThing that) 
    { 
    return this.text.compareTo(that.text); 
    // May need to be more complex if you need to handle nulls, etc 
    } 

    // If you implement compareTo you should override equals ... 
    public boolean equals(Object that) 
    { 
    if (!(that instanceof MyThing)) 
    { 
     return false; 
    } 
    else 
    { 
     MyThing thatThing=(MyThing)that; 
     return this.text.equals(thatThing.text); 
    } 
    } 
} 

等等您可能需要其他的东西取决于你想要做什么。

+0

谢谢!它真的帮助我理解:) –

0

你可以有这种格式

import java.util.ArrayList; 
import java.util.Collections; 


public class MyData implements Comparable<MyData>{ 

private Integer index; 
private String value; 




public MyData(Integer index, String value) { 
    this.index = index; 
    this.value = value; 
} 




/** 
* @return the index 
*/ 
public Integer getIndex() { 
    return index; 
} 




/** 
* @param index the index to set 
*/ 
public void setIndex(Integer index) { 
    this.index = index; 
} 




    /** 
    * @return the value 
    */ 
    public String getValue() { 
     return value; 
    } 




    /** 
    * @param value the value to set 
    */ 
    public void setValue(String value) { 
     this.value = value; 
    } 




public int compareTo(MyData o) { 
    int compare = this.value.compareTo(o.getValue()); 

    if(compare ==0){ 
     compare = this.index.compareTo(o.getIndex()); 
    } 
    return compare; 
} 

/* (non-Javadoc) 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() { 
    return "MyData [index=" + index + ", value=" + value + "]"; 
} 




public static void main(String arg[]){ 


    List<MyData> mySet = new ArrayList<MyData>(); 
    mySet.add(new MyData(1,"FOO")); 
    mySet.add(new MyData(2,"AA")); 
    mySet.add(new MyData(3,"BOB")); 
    mySet.add(new MyData(4,"AA")); 
    mySet.add(new MyData(5,"BOB")); 
    Collections.sort(mySet); 
    System.out.println(mySet); 

} 

}