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

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

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

问题

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

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

public void removeRepeat() {
}

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

public boolean remove(Object element) {
        Node<T> current = head;
        if(isEmpty()) {
            return false;
        }
        if(current.getValue() == element){
            head = head.getNext();
            size--;
            return true;
        }
        while(current.getNext().getValue() != element) {
            current = current.getNext();
            if(current == null) {
                return false;
            }
        }
        if(current.getNext().getNext() == null) {
            tail = current;
        }
        current.setNext(current.getNext().getNext());
        size--;
        return true;
    }

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

public void removeRepeat() {
}

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:

public boolean remove(Object element) {
        Node&lt;T&gt; current = head;
        if(isEmpty()) {
            return false;
        }
        if(current.getValue() == element){
            head = head.getNext();
            size--;
            return true;
        }
        while(current.getNext().getValue() != element) {
            current = current.getNext();
            if(current == null) {
                return false;
            }
        }
        if(current.getNext().getNext() == null) {
            tail = current;
        }
        current.setNext(current.getNext().getNext());
        size--;
        return true;
    }

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中,有四种主要方法可以访问方法内的变量:

  • 将变量作为参数传递:
public void removeRepeat(SomeObject obj)
{
    // 对 obj 做一些操作
}
  • 将变量设置为成员变量(在你的第二段代码片段中 head 看起来就是这样):
SomeObject obj = new SomeObject();

public void removeRepeat()
{
    // 对 obj 做一些操作
}
  • 在方法内部声明变量,即在方法内部部分声明:
public void removeRepeat()
{
    SomeObject obj = new SomeObject();
    // 对 obj 做一些操作
}
  • 在某个地方以静态方式使其可用:
// 其他一些类
public class SomeClass
{
    public static final Object obj = new SomeObject();
}

...

// 然后在你的类中:

public void removeRepeat()
{
    SomeObject obj = SomeClass.obj;
    // 对 obj 做一些操作
}

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

英文:

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

  • Provide the variable as parameter:
public void removeRepeat(SomeObject obj)
{
    // do something with obj
}
  • Have the variable be a member (seems like this is true for head in your second code snippet):
SomeObject obj = new SomeObject();

public void removeRepeat()
{
    // do something with obj
}
  • Declare the variable locally, i.e. inside the method:

public void removeRepeat()
{
    SomeObject obj = new SomeObject();
    // do something with obj
}
  • Have it be available statically somewhere:
// some other class
public class SomeClass
{
    public static final Object obj = new SomeObject();
}

...

// then in your class:

public void removeRepeat()
{
    SomeObject obj = SomeClass.obj;
    // do something with obj
}

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)方法中一样访问这些字段。
例如,

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

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,

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

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:

确定