创建从另一个类对象数组 [英] Creating an Object array from another class

查看:234
本文介绍了创建从另一个类对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这应该很简单。但我一直在这一个很好的小时,想不通为什么它一个NullPointerException异常。

Okay this should be simple. But I have been at this for a good hour and cannot figure out why its a nullpointerexception.

我需要为莎莉创建一个数组,然后测试其长度,这应该是0,这是驱动程序类的主要方法。

I need to create an array for "Sally" and then test its length, which should be 0. This is in the main method of the driver class.

 LendingItem[] sallysItemList = sally.getSignedOutItems();
if (sallysItemList.length == 0)

这是创建出击的对象类。烂B.

and this is in the object class that created sally. that rotten B.

    private LendingItem[] signedOutItems;

public LendingItem[] getSignedOutItems() {
    return signedOutItems;
}

我觉得我需要也许申报贷款项目作为

I feel like I need to maybe declare the Lending item as

private LendingItem[] signedOutItems = {};

但我仍然得到错误与也。

but I still get errors with that also.

编辑:

我要去添加更多,使其更易于理解什么,我需要做发生

Im going to add more so that its more understandable what I need to make happen

以下提供code。

   System.out.println("\n*** Test case #1: Create a CardHolder object & test accessors");
CardHolder sally = new CardHolder("Sally Smith",
                                  152,
                                  "454-1234");
System.out.println("Name:     " + sally.getName()
               + "\nAppt #:   " + sally.getAptNumber()
               + "\nPhone:    " + sally.getPhoneNumber()
               + "\nMember #: " + sally.getMembershipNumber());

LendingItem[] sallysItemList = sally.getSignedOutItems();
if (sallysItemList.length == 0)
  System.out.println("Correct result: Sally has zero lending items.");
else
  System.out.println(">> ERROR: Sally has more than zero lending items.");


 System.out.println("\n*** Test case #6: Sign out one LendingItem");

if(sally.signOut(testItemList[0]))
{ System.out.println("Correct result: Sally signed out an item successfully.");
  sallysItemList = sally.getSignedOutItems();
  if (sallysItemList.length == 1)
    System.out.println("Correct result: Sally has one lending item.");
  else
    System.out.println(">> ERROR: Sally has other than one lending item.");
}
else
  System.out.println(">> ERROR: Sally was unable to sign out an item.");

这是我的code迄今。
刚需返回当前的退出项目。

This is my code thus far. Just need to return the current signed out items.

    public LendingItem[] getSignedOutItems() {
    return signedOutItems;
}

这是我们如何有望加入到我们的阵列,它需要返回一个布尔值

This is how we are expected to add to our array, it needs to return a boolean

    public boolean signOut(LendingItem lendingItem) {
    if (signedOutItems.length < 7) {
        signedOutItems[0] = lendingItem;
        return true;
    } else {
        return false;
    }

不需要彻头彻尾code只是如何真正做到这一点的想法。

Dont need the outright code just an idea of how to actually accomplish this.

推荐答案

这是为了它使用一个数组来维护列表一个疯狂的类,有以不同的大小返回数组的能力的一种方式。

This is one way to make a crazy class which uses an array to maintain the list, with the ability to return arrays with different sizes.

public class CardHolder {
    private LendingItem[] lendingItems;

    public CardHolder () {
        lendingItems = new LendingItem[0];
    }

    public boolean signOut(LendingItem item) {
        if (lendingItems.length >= 7) return false;
        lendingItems = Arrays.copyOf(lendingItems, lendingItems.length + 1); //copies old array, but adding a null value to the new one
        lendingItems[lendingItems.length - 1] = item; //replace the null value with the new item to add
        return true;
    }

    public LendingItem[] getSignedOutItems() {
        return lendingItems;
    }
}

这应该帮助你开始。

这篇关于创建从另一个类对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆