如何从具有大小(n,3)</integer>的LinkedHashSet <integer>中获取单个值

时间:2012-05-31 07:03:53

标签: java

[[58, 89, -50], [58, 50, -89], [89, 50, -58], [-51, -35, 75], [-51, -75, 35], [-35, -75, 51], [-83, -60, -97], [-83, 97, 60], [-60, 97, 83], [null]]

在上面的示例LinkedHashSet<Integer> [size<N><3>]中有N否。整数 对象和每个对象包含3个原始int值。我想访问它 在三个不同的int类型列表中,以便我得到

list1[1] : {58,58,89,-51,.......}
list2[1] : {89,50,50,-35,.......}
list3[1] : {-50,-89,-58,75.......}

正如我在C语言中所做的那样,让P指向我们访问的第一个节点

struct Node *P=Start;
int i=0;
while(P!=NULL){
int a[i]=P->data1;
int b[i]=P->data2;
int c[i]=P->data3;
i++;}

它将如何用Java.Please帮助我。谢谢!Ashish

2 个答案:

答案 0 :(得分:0)

您可以实施3个不同的列表。最简单的方法就是编写一个覆盖java.util.AbstractList<E> http://docs.oracle.com/javase/6/docs/api/java/util/AbstractList.html的类,并从3元素列表中选择一个元素。

这是你的解决方案:

import java.util.ArrayList;

public class DemuxList extends java.util.AbstractList<Integer>{

    ArrayList<Integer[]> listToDemux;
    int listIndex;

    DemuxList(ArrayList<Integer[]> listToDemux, int listIndex) {
        this.listToDemux = listToDemux;
        this.listIndex = listIndex;
    }

    @Override
    public Integer get(int index)
    {
        Integer[] el = listToDemux.get(index);
        if (el != null) {
            return el[listIndex];
        } else {
            return null;
        }        
    }

    @Override
    public int size()
    {
        return listToDemux.size();
    }
}

您可以这样使用它:

ArrayList<Integer[]> fullList = new ArrayList<Integer[]>();
DemuxList l0 = new DemuxList(fullList, 0);
DemuxList l1 = new DemuxList(fullList, 1);
DemuxList l2 = new DemuxList(fullList, 2);

答案 1 :(得分:0)

我解决了问题,代码如下。给予

LinkedHashSet<Set<Integer>> r;
i=0
for(<Set<Integer>> set:r)
    ar[i++]=s.toArray(new Integer[3]);

其中ar是大小为[r.length][3]的2D数组,因为r中的内部集合的大小为3。