简体   繁体   English

简单的Java清单问题

[英]Simple Java List Question

I am supposed to create an iterative method stretch that takes a positive number n as a parameter and returns a new ListItem beginning a list in which each of the numbers in the original list is repeated n times. 我应该创建一个迭代方法拉伸,将正数n作为参数,并返回一个新的ListItem,开始一个列表,其中原始列表中的每个数字都重复n次。 For example, if the original list is ( 6 7 6 9 ) and the parameter value is 2, then the new list returned is ( 6 6 7 7 6 6 9 9 ). 例如,如果原始列表为(6 7 6 9)并且参数值为2,则返回的新列表为(6 6 7 7 6 6 9 9)。

I already have a listitem constructor that has the value at that node and the reference to the next: 我已经有一个listitem构造函数,该构造函数具有该节点的值和对下一个节点的引用:

   ListItem(int number, ListItem next) {
        this.number = number;
        this.next   = next;
}

My code looks like this: 我的代码如下所示:

    public ListItem stretch(int n) {
        //make an array of list items that is n times bigger than the original one.
        ListItem[] newList = new ListItem[this.length() * n];

    //Then loop through the old list one value at a time. At each value do a second loop n times to stretch

        int index = 0;
        int counter = 0;
        for(int i = 0; i < this.length(); i++){
            while(counter++ < n){
                newList[index++] = this[i];*************************
        }
        return newList;****************
    }

}

There are two problem points, at which I starred the lines. 有两个问题要点,在这些问题上我加了注视。 I am supposed to return a listitem, but NewList is an array. 我应该返回一个listitem,但是NewList是一个数组。 ANd I'm not sure what is wrong with the first starred line. 我不确定第一条加星标的行有什么问题。

Any help/guidance would be appreciated. 任何帮助/指导将不胜感激。

The best way to approach this problem is to draw some pictures. 解决此问题的最佳方法是绘制一些图片。 Then try to break the problem into sub-problems. 然后尝试将问题分解为多个子问题。 Let's start with the simple case: a list of length 2: 让我们从一个简单的案例开始:一个长度为2的列表:

ListItem two = new ListItem(1, ListItem(2, null));

Here's one picture 这是一张照片

two = ( number == 1
      ( next   == ( number == 2
                  ( next   == null

Here's another picture: 这是另一张图片:

+---+  +---+    The "/" here is the "null" above, which terminates the list.
| 1 |->| 2 |-/  
+---+  +---+

Think of it this way: a list consists of a first ListItem, which points to the rest of the list via "next". 这样想:一个列表由第一个ListItem组成,它通过“ next”指向列表的其余部分。 The empty list, then is null and the "next" of the last ListItem is always empty. 空列表则为null,最后一个ListItem的“ next”始终为空。 (null). (空值)。

Now what's really going on when we're asked to "stretch" a list? 现在,当我们被要求“拉伸”列表时,实际上发生了什么? Say, by 2? 说2点?

Well, the empty list is easy, it doesn't change. 好了,空列表很容易,它不会改变。 But it's also irrelevant since null.stretch() will end badly in the language you're using. 但这也无关紧要,因为null.stretch()会以您使用的语言结尾很差。 The list of length 1 then is our simplest practical case: 那么长度1的列表就是我们最简单的实际情况:

we have: 我们有:

we have       we want

+---+         +---+   +---+
| 1 |-/       | 1 |-->| 1 |-/
+----         +---+   +---+

Ok, that's not so hard. 好吧,这并不难。 We already have a list of length one. 我们已经有一个长度为1的列表。 All we need to do hang it off the next of a new ListItem and we'll have a list of length two. 我们需要做的就是将它挂在新ListItem的下一个,我们将得到一个长度为2的列表。 Clearly we need the ability to add something to an existing list. 显然,我们需要能够向现有列表添加内容的功能。 Adding it to the front is easiest, so we'll define a little helper for that: 将其添加到最容易的位置,因此我们将为此定义一个小帮手:

ListItem addItemToFront(int number) {
    return new ListItem(number, this);
} 

Ok, now let's code that up and call it stretchFirstItemByOne: 好的,现在让我们对其进行编码,并将其称为stretchFirstItemByOne:

ListItem stretchFirstItemByOne() {           
    return this.addItemToFront(this.number); 
}

You'll see me use this.something() a lot in these examples, though it isn't necessary. 在这些示例中,您会看到我经常使用this.something(),尽管这不是必需的。 I'm just trying to be clear that these are method calls on the current object (this). 我只是想弄清楚这些是当前对象(此)上的方法调用。

But, supposing we want to stretch by some larger n ? 但是,假设我们要延伸更大的n You've already tried -- somewhat haplessly -- to use a for loop above. 您已经尝试过-有点不幸-在上面使用了for循环。 You could do that. 你可以那样做。 But I'm going to do it differently. 但是我将以不同的方式来做。

ListItem stretchFirstItem(n) {
    if (n == 1)      // stretching to length 1 means nothing
        return this; // to do. just return this.
    else {
        // well, if we stretch our item to length n-1 first
        // then all we have to do is stretch it by one and
        // we're done.
        return this.stretchFirstItem(n-1).stretchFirstItemByOne(); 
    }
}

Stop and think about that one. 停下来想一想。 Rewrite it as a for loop if you're having trouble. 如果遇到问题,请将其重写为for循环。

That's all very nice, you might say, but it only handles lists of length one. 您可能会说,这非常好,但是它只能处理长度为1的列表。 How true, how true. 多么真实,多么真实。

Suppose you had a list of length 3 and you wanted to stretch it by 2. 假设您有一个长度为3的列表,并且想将其扩展为2。

+---+  +---+  +---+
( | 1 |->| 2 |->| 3 |-/ ).stretch(2)
  +---+  +---+  +---+

Tough? 强硬? Well, we can get started at least. 好吧,至少我们可以开始。 We know how to handle things if the list has only one item: 如果列表只有一项,我们知道如何处理:

ListItem stretch(int n) {
    ListItem restOfList = this.next;
    if (restOfList == null) { // this list has length one
        return this.stretchFirstItem(n);
    } else {
        // if we had the rest of the list stretched, then we could
        // add this.number to the front of this stretched list, stretch
        // that first item and then we'd be done.
    }
}

Hey, but isn't stretch supposed to do that for us, you know, stretch whole lists? 嘿,但不舒展应该这样做对我们来说,你知道,舒展整个名单? Couldn't we use that to stretch the rest of the list so we can do the easy bit and stretch the first item? 我们不能使用它来拉伸列表的其余部分,以便我们可以轻松地拉伸第一项吗? But we haven't even finished writing stretch yet -- I mean -- it doesn't work. 但是我们甚至还没有完成拉伸的工作-我的意思是-它没有用。 It couldn't be that easy, could it? 不可能那么容易,对吗? Could it? 可以吗

ListItem stretch(int n) {
    ListItem restOfList = this.next;
    if (restOfList == null) { // this list has length one
        return this.stretchFirstItem(n);
    } else {
        return restOfList     //-------------------------
           .magic(...)        // Left as an exercise for
           .moreMagic(...)    // the reader.
           .zyzzy(...);       //-------------------------
    }
}

Homework? 家庭作业?

It looks like you are supposed to be building a linked list but you are instead building an array of ListItems. 看来您应该建立一个链表,但是您要建立一个ListItems数组。 An Array of ListItems isn't bad , but you need to make each ListItem's next value point to the next item in the list. ListItems的数组不错 ,但是您需要使每个ListItem的下一个值指向列表中的下一个项目。

Then the last line of your function will return the first item in the list. 然后,函数的最后一行将返回列表中的第一项。

The first starred line is most certainly wrong, but that is not your problem if you think about it: you are not asked to return an array in the first place. 第一行加星号肯定是错误的,但是如果您考虑一下,那不是您的问题:首先不要求您返回数组。

The problem on how to return a ListItem should solve itself if you consider what your input really looks like: take a sheet of paper, and draw it, and you'll see why returning a single list item is enough, and what do on the ListItem to get each item doubled. 如果考虑输入内容的实际情况,如何返回ListItem的问题应自行解决:拿一张纸,画一下,您将了解为什么返回一个列表项就足够了,以及如何处理列表项。 ListItem使每个项目加倍。

The problem with the first line is that since this isn't an array, you can't subscript it ( [i] ). 第一行的问题在于,由于this不是数组,因此无法下标( [i] )。 I cannot help you with that problem without more details about your ListItem class. 如果没有有关ListItem类的更多详细信息,我将无法帮助您解决该问题。 Are you making a linked list? 您在制作链表吗?

For the return, you probably want to return the first item in the array. 对于返回,您可能想返回数组中的第一项。 If so, change it to return newList[0] . 如果是这样,请将其更改为return newList[0] If you want to return the array, change the function to return the array, like this: public ListItem[] stretch( . 如果要返回数组,请更改函数以返回数组,如下所示: public ListItem[] stretch(

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

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