码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现List数组的几种替代方案

时间:2018-07-02 10:58:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:tin   gen   extends   方案   system   class   定义   extend   str   

在Java中,禁止定义List<Integer>a[],这种List数组结构。
但是还是可以使用其它一些方式来实现列表数组。

一、使用Node把List包裹起来

public class GenericArray {
static class Node {
    public ArrayList<Integer> x;

    public Node() {
        x = new ArrayList<Integer>();
    }
}

public static void main(String[] args) {
    Node[] a = new Node[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = new Node();
        for (int j = 0; j < i; j++) {
            a[i].x.add(j);
        }
    }
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].x.size(); j++) {
            System.out.print(a[i].x.get(j));
        }
        System.out.println();
    }
}
}

二、让Node继承List<Integer>

static class nodenode extends ArrayList<Integer> {
}

public static void main(String[] args) {
    nodenode[] a = new nodenode[10];
    for (int i = 0; i < 10; i++) {
        a[i] = new nodenode();
        for (int j = 0; j < i; j++) {
            a[i].add(j);
        }
    }
    for (int i = 0; i < a.length; i++) {
        for (Integer j : a[i]) {
            System.out.print(j + " ");
        }
        System.out.println();
    }
}

三、使用List<List<>>结构

Java实现List数组的几种替代方案

标签:tin   gen   extends   方案   system   class   定义   extend   str   

原文地址:https://www.cnblogs.com/weiyinfu/p/9252040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!