Java - 如何从类方法返回Int []

时间:2018-03-22 18:14:53

标签: java class methods insert

我有一个问题,在我的程序测试中我必须做作业,theres写List3 MyClass = List2.reverse() List2是一个列表作为数组,我想用我的类创建一个新列表(List3) (MyClass)作为List2反转,我做了反过来但是在我的方法中,如果我写Public MyClass reverse() :我不能返回一个列表,他告诉我,我必须把Public int[] reverse()作为返回列表但是如果我测试器中的做法说List3 MyClass = List2.reverse()不起作用,因为调用的类是int []类型而不是类类型。

public MyList reverse_new()
{
    int[] l = new int[t.length];
    int lunghezzo = t.length - 1;
    for(int x = 0 ; x < t.length ; x++)
    {
        l[x] = t[lunghezzo - x];
    }


    return l;
}

这里他说我必须把int []返回一个列表然后我这样做:

public int[] reverse_new()
{
    int[] l = new int[t.length];
    int lunghezzo = t.length - 1;
    for(int x = 0 ; x < t.length ; x++)
    {
        l[x] = t[lunghezzo - x];
    }


    return l;
}

但是测试人员说我做错了:

public class TestList {
public static void main(String[] args) {

    int[] numeri = {
            100, 200, 300
    };

    MyList L1 = new MyList();
    MyList L2 = new MyList(numeri);
    MyList L3 = L2.reverse_new();

P.S。我无法改变测试员是来自我的老师。我只能使用自己创建的方法。

1 个答案:

答案 0 :(得分:1)

您的代码并不完全清楚,部分内容已丢失,但这应该涵盖相反的情况:

public MyList reverse_new()
{
    int[] l = new int[t.length];
    int lunghezzo = t.length - 1;
    for(int x = 0 ; x < t.length ; x++)
    {
        l[x] = t[lunghezzo - x];
    }

    return new MyList(l);
}

在这种情况下,您将反转列表包装回MyList对象中。

相关问题