我有一个arrayList,它包含多个int数组,这些数组有两个参数,key和value。 (我知道存在一个地图库,但是对于这个任务,我希望使用一个arrayList)。
想象一下,我的arrayList有以下数组:
[3,99] [6,35] [8,9] [20,4] [22,13] [34,10]
正如您所看到的,它们按索引顺序排列,这是在我第一次将它们添加到arrayList时完成的。
如果我想向这个arrayList添加一个数组,它会附加到列表的末尾,而我想将它添加到列表中的正确位置。
我是数组列表的新手,因此想知道是否存在一个我没有遇到过这个问题的优雅解决方案。
目前,我的解决方案是遍历arrayList,然后对于每个数组暂时存储密钥(array [0]),然后我会再次迭代并将我的数组添加到正确的位置(其中的关键是 - 在其他两个键之间)。
答案 0 :(得分:2)
生成一个类来保存两个值并确保实现Comparable
可能更优雅,如下所示:
public class Foo implements Comparable<Foo> {
private int x; // your left value
private int y; // your right value
// Constructor and setters/getters omitted
public int compareTo(Foo o) {
return Integer.compare(x, o.getX());
}
}
然后按如下方式添加和排序:
List<Foo> listOfFoos = new ArrayList<Foo>;
// ...
listOfFoos.add(new Foo(33,55));
Collections.sort(listOfFoos);
这将是最易读的解决方案。可能有更快的选项,但只有在你能证明这部分是一个瓶颈的情况下才会优化。
答案 1 :(得分:2)
你对迭代的想法是正确的;但是不需要执行两次迭代。找到正确的索引并插入元素可以在一个循环中完成。 ArrayList有一个方法add(int, E)
,可以将元素插入列表中的任何位置。试试这个:
//the value you want to insert
int[] toInsert = {someValue, someOtherValue};
//assume theList is the list you're working with
for(int index = 0; index < theList.size() -1; index ++)
{
int key = theList.get(index)[0];
int nextKey = theList.get(index + 1)[0];
//if we've reached the correct location in the list
if (toInsert[0] > key && toInsert[0] < nextKey)
{
//insert the new element right after the last one that was less than it
theList.add(index + 1,toInsert);
}
}
请注意,此方法假定列表已开始排序。如果您想要保证,请查看描述排序和Comparator
的其他一些答案。
答案 2 :(得分:1)
第一个选项
如果您希望能够对数组进行排序,则应该存储Comparable个对象。
因此,您可以创建一个Class,它将保存您的两个值数组并实现Comparable接口。
如果您选择此选项,则在添加元素后,您只需在.sort()
上致电List
。
第二个选项
您可以定义可用于排序的Comparator。这将是可重用的,并允许您保留您的二维数组。每次添加后,您还必须进行排序。
第三个选项
您可以动态定义您的比较器,如以下特定问题所示: Java Comparator class to sort arrays
答案 3 :(得分:0)
您可以执行以下操作:
import java.util.ArrayList;
public class AddElementToSpecifiedIndexArrayListExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
/*
To add an element at the specified index of ArrayList use
void add(int index, Object obj) method.
This method inserts the specified element at the specified index in the
ArrayList.
*/
arrayList.add(1,"INSERTED ELEMENT");
System.out.println("ArrayList contains...");
for(int index=0; index < arrayList.size(); index++)
System.out.println(arrayList.get(index));
}
}
/ * 输出将是 ArrayList包含...... 1
INSERTED ELEMENT
2
3
* /
答案 4 :(得分:0)
还有一个add
版本,它采用添加新项目的索引。
int i;
for(i=0; i<arr.size(); i++){
if(arr.get(i)[0] >= newArr[0]){
arr.add(i, newArr);
}
}
if(i == arr.size())
arr.add(i, newArr)
答案 5 :(得分:0)
使用int []的比较器和binarySearch:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main
{
public static void main(String[] argv)
{
ArrayList<int[]> list = new ArrayList<int[]>();
list.add(new int[] { 3, 99 });
list.add(new int[] { 6, 35 });
list.add(new int[] { 8, 9 });
list.add(new int[] { 20, 4 });
list.add(new int[] { 22, 13 });
list.add(new int[] { 34, 10 });
Compar compar = new Compar();
addElement(list, new int[] { 15, 100 }, compar);
for(int[] t : list)
{
System.out.println(t[0]+" "+t[1]);
}
}
private static void addElement(ArrayList<int[]> list, int[] elem, Compar compar)
{
int index = Collections.binarySearch(list, elem, compar);
if (index >= 0)
{
list.add(index, elem);
return;
}
list.add(-index - 1, elem);
}
static class Compar implements Comparator<int[]>
{
@Override
public int compare(int[] a, int[] b)
{
return a[0] - b[0];
}
}
}