循环时为链表创建名称(Java)(creating names for linked lists while looping (Java))

编程入门 行业动态 更新时间:2024-10-27 22:34:38
循环时为链表创建名称(Java)(creating names for linked lists while looping (Java)) java

我已经实现了一个简单的链表类,我现在想在循环中使用它。 我想知道如何在循环的每次迭代中最好地将名称分配给列表。

本质上我循环一些整数,我想只给每个列表的整数名称,但我不能说

列表i =新列表();

对 ?

可能有一种简单的方法可以做到这一点,但我不确定如何,并将感激不尽

I have implemented a simple linked list class and I would now like to use it in a loop. I am wondering how to best assign names to the list in each iteration of the loop.

Essentially I am looping over some integers, and I would like to just give each list the name of that integer, but I cannot say

List i = new List();

right ?

There probably is an easy way to do this, but I m not sure how, and would be grateful for

最满意答案

我认为你将变量的作用与集合的作用混为一谈。 根据您的问题,我收集您要为循环中的每个索引创建一个列表,并且您希望以后能够通过其索引访问该列表:

ArrayList<LinkedList<String>> listOfLists = new ArrayList<LinkedList<String>>(); for(int i = 0; i < 10; i++) { LinkedList<String> list = new LinkedList(); listOfLists.add(list); // do stuff to the list... } // access LinkedList<String> thirdList = listOfLists.get(2); // index 2 = third entry

所以你看,LinkedLists没有根据i的值命名 ,但你仍然可以通过给定的i值来访问它们。

I think you're confusing the role of variables with the role of collections. From your question I gather that you want to create a list for each index in your loop, and you would like to later be able to access that list by its index:

ArrayList<LinkedList<String>> listOfLists = new ArrayList<LinkedList<String>>(); for(int i = 0; i < 10; i++) { LinkedList<String> list = new LinkedList(); listOfLists.add(list); // do stuff to the list... } // access LinkedList<String> thirdList = listOfLists.get(2); // index 2 = third entry

So you see, the LinkedLists are not named according to the value of i, but you can still access them by a given value of i.

更多推荐

本文发布于:2023-04-28 08:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链表   名称   Java   creating   looping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!