English 中文(简体)
在Android获得古老的阵列物品位置
原标题:Getting old positions of ArrayList items in Android
  • 时间:2012-05-22 14:44:02
  •  标签:
  • java
  • android

我有一个名为 play_viewCount 的阵列列表: 我正在对这个阵列列表进行排序, 并将其存储在一个新的阵列列表中 。

现在我已经整理了阵列列表:但是我想要的是,在对阵列列表中新项目的位置进行排序之前?

ArrayList<String> sort_play_viewCount = play_ViewCount; // here play_viewCount is ArrayList
ArrayList<Integer> position_array = new ArrayList<Integer>(); 
System.out.println("......................................... Play Count :"+sort_play_viewCount);
Collections.sort(sort_play_viewCount);
System.out.println(".........................................sort Play Count :"+sort_play_viewCount);

for(int j = 0; j<sort_play_viewCount.size(); j++){
    for(int k = 0; k<sort_play_viewCount.size(); k++){
        if(play_ViewCount.contains(sort_play_viewCount.get(j))){
            position_array.add(k);
        }
    }
}

System.out.println(" .................Position Array: "+position_array);

有谁知道在排序前如何获得新项目的位置吗?

最佳回答

尝试做一些不同的尝试:

ArrayList<Integer> position_array = new ArrayList<Integer>(); 

position_array.addAll(play_viewCount);
Collections.sort(position_array);

position_array 已被排序, 要获得之前的位置, 您可以调用 play_viewCount.indexof( value);

问题回答

您可以将 ArrayList 的元素放入 Map< String, Integer> (由 HashMap< String, Integer> 执行) 的 map< 元素中,其中条目的密钥是 string 元素来自 ArrayList , 值为 Integer 代表位置。

Map<String, Integer> originalPositions = new HashMap<String, Integer>();

String item = ...
String position = ...
originalPositions.put(item, position);

// do something with the ArrayList, such as sorting
Collections.sort(arrayList);

String someItem = arrayList.get(i);
int originalPosition = originalPositions.get(someItem);

(笑声) 顺便说一句,你的代码片断的这条线 并没有做你认为它能做的 /强 :

ArrayList<String> sort_play_viewCount = play_ViewCount;

它不创建与原始目录内容相同的新矩阵列表。 相反, 它只是创建了对原始矩阵列表的新引用 。 play_ViewCount >>sort_play_viewCount 都指同一个对象, 换句话说, 对其中一个变量的任何修改( 如排序) 也会影响另一个变量 。

要创建 ArrayList 的新副本( 然而, 仍然浅), 请使用以下语义 :

ArrayList<Integer> original = ...
ArrayList<Integer> copy = new ArrayList<Integer>(original);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签