简体   繁体   English

更有效,更紧凑的方法是:大量的链表变量集或包含每个变量的二维数组列表?

[英]What's more efficient and compact: A huge set of linkedlist variables or a two-dimensional arraylist containing each of these?

I want to create a large matrix (n by n) where each element corresponds to a LinkedList (of certain objects). 我想创建一个大矩阵(n乘n),其中每个元素对应于(某些对象的)LinkedList。

I can either 我可以

  1. Create the n*n individual linked lists and name them with the help of eval() within a loop that iterates through both dimensions (or something similar), so that in the end I'll have LinkedList_1_1, LinkedList_1_2 etc. Each one has a unique variable name. 创建n * n个单独的链表,并在循环遍历这两个维度(或类似内容)的循环中借助eval()对其进行命名,以便最终获得LinkedList_1_1,LinkedList_1_2等。每个链表都有一个唯一变量名。 Basically, skipping the matrix altogether. 基本上,完全跳过矩阵。

  2. Create an ArrayList of ArrayLists and then push into each element a linked list. 创建一个ArrayLists的ArrayList,然后将一个链接列表推入每个元素。

Please recommend me a method if I want to conserve time & space, and ease-of-access in my later code, when I want to reference individual LinkedLists. 如果要节省时间和空间,并且在以后的代码中要引用单个LinkedList时更易于访问,请向我推荐一种方法。 Ease-of-acess will be poor with Method 1, as I'll have to use eval whenever I want to access a particular linked list. 方法1的易用性较差,因为每当我要访问特定的链接列表时,都必须使用eval。

My gut-feeling tells me Method 2 is the best approach, but how exactly do I form my initializations? 我的直觉告诉我方法2是最好的方法,但是如何准确地进行初始化?

As you know the sizes to start with, why don't you just use an array? 您知道开始的大小,为什么不只使用数组呢? Unfortunately Java generics prevents the array element itself from being a concrete generic type, but you can use a wildcard: 不幸的是,Java泛型阻止了数组元素本身成为具体的泛型类型,但是您可以使用通配符:

LinkedList<?>[][] lists = new LinkedList<?>[n][n];

Or slightly more efficient in memory, just a single array: 或仅在单个阵列上的内存效率稍高

LinkedList<?>[] lists = new LinkedList<?>[n * n];

// Then for access...
lists[y * n + x] = ...;

Then you'd need to cast on each access - using @SuppressWarnings given that you know it will always work (assuming you encapsulate it appropriately). 然后,您需要进行每次访问-使用@SuppressWarnings@SuppressWarnings是您知道该访问将始终有效(假设您对其进行了适当封装)。 I'd put that in a single place: 我把它放在一个地方:

@SuppressWarnings("unchecked")
private LinkedList<Foo> getList(int x, int y) {
    if (lists[y][x] == null) {
        lists[y][x] = new LinkedList<Foo>();
    }
    // Cast won't actually have any effect at execution time. It's
    // just to tell the compiler we know what we're doing.
    return (LinkedList<Foo>) lists[y][x];
}

Of course in both cases you'd then need to populate the arrays with empty linked lists if you needed to. 当然,在两种情况下, 如果需要,您都需要使用空链表填充数组。 (If several of the linked lists never end up having any nodes, you may wish to consider only populating them lazily.) (如果几个链表永远都不会有任何节点,那么您可能希望考虑仅懒惰地填充它们。)

I would certainly not generate a class with hundreds of variables. 当然不会生成具有数百个变量的类。 It would make programmatic access to the lists very painful, and basically be a bad idea in any number of ways. 这将使对列表的编程访问变得非常痛苦,并且从任何方面讲,从根本上来说都是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM