如何在Java中创建一个空参数列表的方法

huangapple go评论89阅读模式
英文:

How to create a method with an empty parameter list in java

问题

我正在脑子一片空白,出于某种原因我无法弄清楚如何编写一个参数为空的方法。

我这里有一个方法(空白)

  1. public void removeRepeat() {
  2. }

这是针对链表的,我想要删除重复项。我下面有其他方法,我在考虑使用它们来实现:

  1. public boolean remove(Object element) {
  2. Node<T> current = head;
  3. if(isEmpty()) {
  4. return false;
  5. }
  6. if(current.getValue() == element){
  7. head = head.getNext();
  8. size--;
  9. return true;
  10. }
  11. while(current.getNext().getValue() != element) {
  12. current = current.getNext();
  13. if(current == null) {
  14. return false;
  15. }
  16. }
  17. if(current.getNext().getNext() == null) {
  18. tail = current;
  19. }
  20. current.setNext(current.getNext().getNext());
  21. size--;
  22. return true;
  23. }

我还有一个设置方法等等,但我的问题通常是如何编写没有参数的方法。我只需创建一个新变量吗?我如何访问它要我反转的列表?我考虑使用一个循环来检查重复项,然后删除重复的元素,但我不确定如何构建这个循环。
for (int i=0; i< ???; i++)

英文:

I'm having a brain fart and for some reason I can't figure out how to write a method where the parameters are empty.

I have a method here (empty)

  1. public void removeRepeat() {
  2. }

This is for a linked list and I want to remove repeats. I have other methods below that I'm thinking of using to go about it:

  1. public boolean remove(Object element) {
  2. Node&lt;T&gt; current = head;
  3. if(isEmpty()) {
  4. return false;
  5. }
  6. if(current.getValue() == element){
  7. head = head.getNext();
  8. size--;
  9. return true;
  10. }
  11. while(current.getNext().getValue() != element) {
  12. current = current.getNext();
  13. if(current == null) {
  14. return false;
  15. }
  16. }
  17. if(current.getNext().getNext() == null) {
  18. tail = current;
  19. }
  20. current.setNext(current.getNext().getNext());
  21. size--;
  22. return true;
  23. }

I also have a set method and whatnot, but my question is generally how to write the method if there are no parameters. Do I just create a new variable? How do I access the list it wants me to reverse? I'm thinking to have a for loop to check for repeats and then remove the repeating element, but I'm not sure how to construct the for loop.
for (int i=0; i&lt; ???; i++)

答案1

得分: 1

总体而言,在Java中,有四种主要方法可以访问方法内的变量:

  • 将变量作为参数传递:
  1. public void removeRepeat(SomeObject obj)
  2. {
  3. // 对 obj 做一些操作
  4. }
  • 将变量设置为成员变量(在你的第二段代码片段中 head 看起来就是这样):
  1. SomeObject obj = new SomeObject();
  2. public void removeRepeat()
  3. {
  4. // 对 obj 做一些操作
  5. }
  • 在方法内部声明变量,即在方法内部部分声明:
  1. public void removeRepeat()
  2. {
  3. SomeObject obj = new SomeObject();
  4. // 对 obj 做一些操作
  5. }
  • 在某个地方以静态方式使其可用:
  1. // 其他一些类
  2. public class SomeClass
  3. {
  4. public static final Object obj = new SomeObject();
  5. }
  6. ...
  7. // 然后在你的类中:
  8. public void removeRepeat()
  9. {
  10. SomeObject obj = SomeClass.obj;
  11. // 对 obj 做一些操作
  12. }

我建议你对一些Java基础知识有一定了解,这可以消除你对这方面可能存在的一些误解。

英文:

Broadly speaking there four major ways you can access a variable inside a method in Java:

  • Provide the variable as parameter:
  1. public void removeRepeat(SomeObject obj)
  2. {
  3. // do something with obj
  4. }
  • Have the variable be a member (seems like this is true for head in your second code snippet):
  1. SomeObject obj = new SomeObject();
  2. public void removeRepeat()
  3. {
  4. // do something with obj
  5. }
  • Declare the variable locally, i.e. inside the method:
  1. public void removeRepeat()
  2. {
  3. SomeObject obj = new SomeObject();
  4. // do something with obj
  5. }
  • Have it be available statically somewhere:
  1. // some other class
  2. public class SomeClass
  3. {
  4. public static final Object obj = new SomeObject();
  5. }
  6. ...
  7. // then in your class:
  8. public void removeRepeat()
  9. {
  10. SomeObject obj = SomeClass.obj;
  11. // do something with obj
  12. }

I recommend getting over some Java basics, it should clear up some misconceptions you might have about this.

答案2

得分: 0

根据您的remove(Object element)方法,您有一个包含headtail字段以及一个size字段的类。

如果您要在同一类中添加一个新的方法removeRepeat(),那么您可以像在remove(Object)方法中一样访问这些字段。
例如,

  1. public void removeRepeat() {
  2. // 对类的“head”字段进行方法内部的复制。
  3. Node<T> current = head;
  4. // 在此遍历列表:
  5. while (current != null) {
  6. // 做一些操作
  7. current = current.getNext();
  8. }
  9. // 等等...
  10. }
英文:

Judging by your remove(Object element) method, you have a class that contains a head and a tail field, as well as a size field.

If you are adding a new method removeRepeat() to the same class, then you can go ahead and access these fields just like in the remove(Object) method.
For example,

  1. public void removeRepeat() {
  2. // Make a method-local copy of the class&#39;s &quot;head&quot; field.
  3. Node&lt;T&gt; current = head;
  4. // Iterate through the list here:
  5. while (current != null) {
  6. // do something
  7. current = current.getNext();
  8. }
  9. etc...
  10. }

huangapple
  • 本文由 发表于 2020年9月19日 00:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63959279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定