如何在java中创建一个链表数组?
发布时间:2020-12-14 05:57:28 所属栏目:Java 来源:网络整理
导读:所以我需要像这样输入一个二分图的边: 61 31 21 52 72 42 9 第一个数字是边数.之后列出边.看到如何顶点1有多个不同的边缘,我想跟踪1连接到什么,我在想,图的每个顶点都会有一些列表,它连接的顶点,导致我尝试创建一个链表的数组,但我不知道该怎么做.我试过了
所以我需要像这样输入一个二分图的边:
6 1 3 1 2 1 5 2 7 2 4 2 9 第一个数字是边数.之后列出边.看到如何顶点1有多个不同的边缘,我想跟踪1连接到什么,我在想,图的每个顶点都会有一些列表,它连接的顶点,导致我尝试创建一个链表的数组,但我不知道该怎么做.我试过了 LinkedList<Integer>[] vertex = new LinkedList[5]; int i = 0,m = 6; while(i!=m){ int temp = sc.nextInt(); int temp2 = sc.nextInt(); vertex[temp].add(temp2); i++; } 但是我在加法行得到一个nullpointerexception. 解决方法LinkedList<Integer>[] vertex = new LinkedList[5]; int i = 0,m = 6; while(i!=m){ int temp = sc.nextInt(); int temp2 = sc.nextInt(); // Make sure the list is initialized before adding to it if (vertex[temp] == null) { vertex[temp] = new LinkedList<Integer>(); } vertex[temp].add(temp2); i++; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |