【问题标题】:Printing select sections of an array list if a condition is met如果满足条件,则打印数组列表的选定部分
【发布时间】:2015-04-13 20:17:15
【问题描述】:

我正在编写一个程序,允许用户将狗签入狗窝,但我在打印所有喜欢骨头的狗时遇到了一些麻烦(添加狗时数组列表中的标准之一)。当您从主控制台菜单中选择“打印所有喜欢骨头的狗”选项时,我试图仅打印名称,但它正在打印数组列表中的所有信息。

这是当前代码:

private void printDogsWithBones() {

    Dog[] dogsWithBones = kennel.obtainDogsWhoLikeBones();
    System.out.println("Dogs with bones: ");

    for (Dog d: dogsWithBones){
        System.out.println(d);
    }   
}

public Dog[] obtainDogsWhoLikeBones() {    
    // TODO
    // Prints "null" if a dog is in the array that doesn't like bones.

    Dog[] tempResult = new Dog[dogs.size()];
    // Sets the int tempResult to -1 to allow for scanning through the array from position 0, making sure every dog is accounted for.
    int tempCount = -1;

    // For each loop to scan through the array and check for each dog that likes bones
    for (Dog t : dogs){
        // Adds 1 to tempCount to enable efficient and functional scanning through the array
        tempCount++;
        // Adds the animal from the array to the temp Array which will then be printed back in KennelDemo
        if(t.getLikesBones() == true){
            tempResult[tempCount] = t;
        }
    }
    return tempResult;
}

我遇到的另一个问题是,如果狗不喜欢骨头,它会打印出“null”而不是什么都没有。

这是运行该方法时控制台打印的内容:

1 -  add a new Dog 
2 -  set up Kennel name
3 -  print all dogs who like bones
4 -  search for a dog
5 -  remove a dog
6 -  set kennel capacity
q - Quit
What would you like to do:
3
Dogs with bones: 
null
Dog name:RoverLikes Bones?:trueOriginal Owner:[David         98765]Favfood:NipplesFoodPerDay:3
Dog name:IzzyLikes Bones?:trueOriginal Owner:[Jay 123456789]Favfood:CurryFoodPerDay:3

提前谢谢大家。

【问题讨论】:

    标签: java arraylist printing


    【解决方案1】:

    那是因为您在 tempResult 数组中跳过了狗不喜欢骨头的数组索引。即使狗不喜欢骨头,您也会增加 tempCount ,因此将跳过当前值。要解决此问题,请在 if 语句中移动 tempCount 增量。

    if(t.getLikesBones() == true){
        tempCount++;
        tempResult[tempCount] = t;
    }
    

    此外,由于 t.getLikesBones() 将评估为 true,因此您不需要 == true。您也可以在数组索引上使用前缀增量,但这可能会降低可读性。

    if(t.getLikesBones()){
        tempResult[++tempCount] = t;
    }
    

    在创建时不知道数组大小时使用数组通常表明您应该使用列表。这将为您动态调整自身大小。如果您想转换回一个很好的数组,但列表而不是数组可能更适合您的整个应用程序

    public Dog[] obtainDogsWhoLikeBones() {    
    
        // Initialise dogsWhoLikeBones list
        ArrayList<Dog> dogsWhoLikeBones = new ArrayList<Dog>();
    
        // Iterate through dogs
        for (Dog dog : dogs){
            if(dog.getLikesBones()){
                // If a dog likes bones, add them to the list
                dogsWhoLikeBones.add(dog);
            }
        }
    
        // Get size of list to set the size of the return array
        int listSize = dogsWhoLikeBones.size();
    
        // Convert dogs list back to an array
        return dogsWhoLikeBones.toArray(new Dog[listSize]);
    }
    

    要打印出狗的名字而不是 java 试图将你的对象变成一个字符串,在你的打印 for 循环中你需要得到它的名字。您的狗类中是否有此访问器(例如 getName)?如果是这样,您可以这样做:

    for (Dog d: dogsWithBones){
        System.out.println(d.getName());
    }   
    

    【讨论】:

    • 如果 likeBones 的值返回 false,这不会导致 tempCount 永远不会增加吗?谢谢你的回答!
    • 刚刚编辑并运行了代码,它现在像以前一样打印数组中的所有内容,但在底部而不是在控制台消息的开头打印“null”。
    • @JayGould 是的,就是这样。您只使用 tempCount 来访问 tempResult,因此您只希望在插入新项目时不增加它。您现在得到空值,因为狗数组比喜欢骨头的狗数组大,所以您有两个选择,要么调整数组大小,要么改用列表。如果您在创建数组时不知道数组有多大,那么列表通常是一个更好的主意。列表是一种选择吗?如果是这样,我会相应地更新我的答案。
    • 目前我可以使用的东西没有任何限制,更多的是打印出正确的数据而不是使用的方法。老实说,我可以为这一切设置一个巨大的扫描方法并这样做,但我宁愿让它有效地完成。您介意解释一下列表方法吗?
    • 另外,我希望只打印出狗的名字,而不是数组中与狗相关的所有信息,这可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多