English 中文(简体)
使用来自其他类的阵列的 Java [关闭]
原标题:Java using an array from another class [closed]

我是新来爪哇的 所以请温柔点...

考虑以下 SchoppingList 类 :

public class ShoppingList {
...
    public ItemPrices[] getSortedPrices(){
        //do sorting stuff here etc
        return ret.toArray(new ItemPrices[0]);
    }
}

现在我有另一个班级叫做 hello :

public class Hello {
...
    private Groceries createGroceries() {
    ...
         pricearray[] =  ShoppingList.ItemPrices[] //????
    ...
    }
}

我要指定我创建的数组价格数组, 以等同于在方法中返回的 ProjectPrice 数组 。

但是,我没有得到我想要的,什么才是这样做的正确方法?

最佳回答

除非该方法 getSortedPrices 是一种静态方法, 您需要从 SwessList 类的示例中调用它, 所以您应该创建以下实例

public class Hello {
...
    private Groceries createGroceries() {
    ...
        ShoppingList sList = new ShoppingList();
        PriceList [] pricearray =  sList.getSortedPrices() //you call a method by its name, not return type.
    ...
    }
}

而且,我不明白如何

(项目收费是双倍)。

is it supposed to be an array of doubles, or an array of instance of the class ItemPrices? if its supposed to be an array of doubles, you need to do this:

public class ShoppingList {
...
    public double[] getSortedPrices(){
        //do sorting stuff here etc
        return new double[n] // n is the length of the array
    }
}

线条和直线

PriceList [] 价格阵列 = sist.getSordtedPrices ()

应该是

双 [] 价格阵列 = sist.getSordedPrices ()

问题回答

你不必专注于其他的问题, 你必须做一些事情,比如

ShoppingList sl = new ShoppingList();
ItemPrices[] pricearray =  sl.getSortedPrices();

但这需要你们知道类型、构建器、阵列, 如何称呼方法和其他许多东西!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签