在链接列表数组中查找最长链接列表

时间:2013-04-11 21:59:27

标签: java arrays for-loop linked-list

我有一个链接列表数组(一个邻接列表),链接列表的大部分长度都是4,但有一些会随机拥有更多。我的目标是遍历数组并找到长度超过4的数组(这就是容易的部分)然后将索引添加到数组中,这样就像

for (int i = 0; i < 1000; i++){
    if(sWorld[i].length > 4)
         //add i to an array
         // then sort the array

无法真正弄清楚如何做到这一点。我试图添加到一个墨水列表然后链接列表toArray()但它然后搞砸了。我真的不知道如何在我的sWorld数组中添加'i'点来说明新数组中的第一个位置即将用于比4号更大的数据。

非常感谢任何帮助!

编辑澄清一个位

我需要大小&gt;的位置索引4,但后来我想知道我得到的哪些索引具有最大的尺寸。也许我在我的操作中并不是100%清楚,但基本上我试图找到阵列中1000个索引中哪个连接最多(链接最长的列表)有意义?

我想知道数组的前10个连接索引(也就是10个链表最大的链接)

2 个答案:

答案 0 :(得分:3)

您可以使用ArrayList存储索引:

List<Integer> indexes = new ArrayList<Integer>();

for (int i = 0; i < 1000; i++){
    if (sWorld[i].length > 4) {
         //add i to a list (not an array yet)
         indexes.add(i);
    }
    ...
}
// then sort the list
// not necessary, as indexes are inserted in the right order, but if you must...
// Collections.sort(indexes);

// and, if you need an array instead of a list
Integer[] indexesArray = indexes.toArray(new Integer[indexes.size()]);

ListArrayList用作可变长度数组。虽然没有实际数组那么有效。

如上所示,以后无需对数组进行排序,但如果必须,可以使用Collections.sort()

另外,如果您必须有int[]而不是Integer[],请检查:How to convert List<Integer> to int[] in Java?

<强>更新

如果你想知道较大数组的大小和索引,这是一个全新的问题。以下是处理它的工作代码。

基本上,每次找到大小超过4的数组时,都会在列表中添加一对(index, size)。然后按大小按降序排列此列表。

main()方法结束时,创建一个数组(int[] topTenIndexes),其中包含10个最大数组的索引(索引按其数组长度的降序显示) )。当没有足够的大(长度> 4)数组时,结果是-1。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Example {

    public static void main(String[] args) {
        List<ArrayIndexAndSize> indexes = new ArrayList<ArrayIndexAndSize>();

        int[][] sWorld = {{1},{2,5,5,5,5},{3,6,6,6,6,6}};
        for (int i = 0; i < sWorld.length; i++){
            if (sWorld[i].length > 4) {
                 // add a pair (index, size) to the list
                 indexes.add(new ArrayIndexAndSize(i, sWorld[i].length));
            }
            //...
        }
        // then sort the list by array SIZE, in descending order
        Collections.sort(indexes);
        // Print it!
        System.out.println(indexes);
        /* output:
        "[[Array index: 2; Array size: 6], [Array index: 1; Array size: 5]]"
         */

        // Generating an array with the top ten indexes
        int[] topTenIndexes = new int[10];
        Arrays.fill(topTenIndexes, -1);
        for (int i = 0; i < indexes.size() && i < 10; i++) {
            topTenIndexes[i] = indexes.get(i).index;
        }
        // Print it
        System.out.println(Arrays.toString(topTenIndexes));
        /* output: [2, 1, -1, -1, -1, -1, -1, -1, -1, -1] */
    }

    public static class ArrayIndexAndSize implements Comparable<ArrayIndexAndSize> {
        public int index;
        public int size;
        public ArrayIndexAndSize(int index, int size) {
            this.index = index;
            this.size = size;
        }
        /* Order by size, DESC */
        /* This is called by Collections.sort and defines the order of two elements */
        public int compareTo(ArrayIndexAndSize another) {
            int thisVal = this.size;
            int anotherVal = another.size;
            return -(thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
       }
        @Override
        public String toString() {
            return "[Array index: "+index+"; Array size: "+size+"]"; 
        }
    }

}

答案 1 :(得分:0)

如果您有一个数组LinkedList<?>[] sWorld(填写您在?中使用的类型),请执行以下操作:

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

for (int i=0; i<sWorld.size(); i++) {
    if (sWorld[i].size > 4) {
        listOfLists.add(i);
    }
}
Comparator<Integer> sizeComparator = new Comparator<Integer>() {
    public int compare(Integer a, Integer b) {
        return Integer.compare(sWorld[a].size(), sWorld[b].size());
    }
}
Collections.sort(listOfLists, sizeComparator);
相关问题