java - 在链表数组中找到最长的链表

标签 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 的数组。

任何帮助将不胜感激!

编辑以澄清一点

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

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

最佳答案

您可以使用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+"]"; 
        }
    }

}

关于java - 在链表数组中找到最长的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15959625/

相关文章:

java - 你如何测试 cookie 是否在 Java/J2EE 中启用

javascript - 将选项附加到下拉列表时删除重复项

javascript - 如何从数组中获取不匹配的元素?

java - 每个循环替换变量名称

java - Project Euler 14(Collat​​z 猜想),Java 中实现缓存的问题

java - 定义命名空间时,使用 Maven 创建的架构验证 XML 失败

python - 将数字转换为具有固定长度的二进制

c++ - 错误的 'for' 循环结果 C++

java - 定义 Google App Engine Java 后端 - 未显示在管理控制台中

c - 如何在 C 编程中取消引用多维数组?