简体   繁体   English

如何从初始化 LinkedList 的不同类向 LinkedList 添加元素?

[英]How can I add elements to a LinkedList from a different class that the LinkedList is initialized in?

I'm still relatively new to java programming.我对java编程还是比较陌生的。 I know you use the dot (.) operator when using methods of specific classes, but I was wondering how to add elements to a LinkedList if I'm in a seperate class.我知道您在使用特定类的方法时会使用点 (.) 运算符,但我想知道如果我在一个单独的类中,如何将元素添加到 LinkedList。

I initialize the LinkedList in a class.我在一个类中初始化 LinkedList。

Then in a seperate class's initializer, I get the information I need in order to add elements to that class.然后在单独的类的初始值设定项中,我获得了向该类添加元素所需的信息。 Now that I have that information I would like to do something such as现在我有了这些信息,我想做一些事情,比如

listname.add(objectType object); 

but I get a "cannot find symbol" error但我收到“找不到符号”错误

For an amateur programmer like myself, it seems to be a scoping problem where it cannot see the LinkedList so I tried using a fully defined name对于像我这样的业余程序员来说,这似乎是一个范围问题,它看不到 LinkedList 所以我尝试使用完全定义的名称

className.listname.add(objectType object); 

But this didn't work either, If anyone has a few minutes where they could help me out with this issue I would very very much appreciate it.但这也不起作用,如果有人有几分钟的时间可以帮助我解决这个问题,我将非常感激。 Thank you very much in advance非常感谢您提前

Assume that the list is of type Object .假设列表的类型是Object Then, you have to do the following to add an element to the list.然后,您必须执行以下操作才能将元素添加到列表中。

Object obj=new Object();
listname.add(obj);

You have to initialize the element before adding it to the LinkedList.在将元素添加到 LinkedList 之前,您必须对其进行初始化。

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

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