zoukankan      html  css  js  c++  java
  • Java实现List数组的几种替代方案

    在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<>>结构

  • 相关阅读:
    sed 使用总结
    rgmanager 介绍
    linux nbd & qemu-nbd
    corosync基本使用
    svn 基本使用
    tornado 实践
    mysql mac启动
    postgresql
    django
    django
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/9252040.html
Copyright © 2011-2022 走看看