将对象添加到Java数组中

时间:2019-06-15 10:38:37

标签: java

我是Java的新手,并下了一个任务,该任务构造带有数组“ supplements”的“ magazine”类。Supplements带有名称和成本。我想弄清楚的是,如何在“杂志”下的数组中添加3种补品。

这是“补充”的代码

public class Supplements {
 String Supplement;
 double cost;

public Supplements ()
{
    Supplement="abcdef";
    cost= 10;

}
public Supplements(String sp, double ct )
{
    Supplement = sp;
    cost=ct;
}

public String getSupplement()
{
    return Supplement;
}
public void setSupplement(String sp)
{
  Supplement=sp;
}
public void setcost(double ct)
{
    cost=ct;
}
public double getcost()
{
    return cost;
}
public String toString()
{
    return ("Supplements: "+ Supplement + "\n" + "Weekly cost: "+ cost);
}

“杂志”的开头。假设有成本和一系列补品(s1 =“ abc”,10;,s2 =“ def”,11;等)

public class Magazine {
double cost;
Supplements[] supplist;

public Magazine ()
{

    cost=20;
   Supplements[] supplist= new Supplements[1];

如果这听起来像胡言乱语,我感到很抱歉,我没有太多的编码经验

5 个答案:

答案 0 :(得分:2)

数组在Java中具有固定大小。您必须使用ArrayList来支持动态尺寸。

答案 1 :(得分:0)

做这样的事情

Supplements s1 = new Supplements("abc", 10);
Supplements s2 = new Supplements("def", 20);
Supplements s3 = new Supplements("hij", 30);

Supplements[] supplist= new Supplements[3];
supplist[0] = s1;
supplist[1] = s2;
supplist[2] = s3;

答案 2 :(得分:0)

如果您先验地知道数组将包含多少个元素,则可以使用以下方法创建它:

Supplements[] arr = new Supplements[n];

其中n是它将拥有的元素数。然后,您可以使用索引为其分配元素:

arr[0] = new Supplements("abc", 10);
arr[1] = new Supplements("def", 11);

请注意,01是索引。您可以访问直到n-1,因为数组的大小为n,索引从0开始。

如果您不知道将拥有多少元素,则应考虑使用Java Collections中的数据结构,例如LinkedListArrayList

答案 3 :(得分:0)

首先Supplements[] supplist= new Supplements[1];无效,如果您已经声明Supplements[] supplist;,则会给出重复变量的错误


现在要在数组中添加数据

    Supplements[] supplist= new Supplements[3];
    supplist[0] = new Supplements("abc", 10);
    supplist[1] = new Supplements("def", 20);
    supplist[2] = new Supplements("hij", 30);

,并且在Java中,数组具有固定大小,建议使用 ArrayList 的Object而不是Array来支持动态大小。 例如

ArrayList<Supplements> supplist= new ArrayList<Supplements>();
supplist.add(new Supplements("abc", 10));
supplist.add(new Supplements("def", 20));
supplist.add(new Supplements("hij", 30));

您可以在列表中添加动态的补品

答案 4 :(得分:0)

我建议您使用ArrayList而不是Array,除非您需要或必须使用Array来完成此任务,如果必须使用Array,则它应该像这样简单:

www

然后,您只需将其添加到Magazines数组中,但要算入java中的数组具有固定大小,因此可以说,如果它的固定大小为1,那么您只能像这样添加1个补充:

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data android:scheme="https" android:host="example.in" />
    <data android:scheme="https" android:host="www.example.in" />
    <data android:scheme="http" android:host="example.in" />
    <data android:scheme="http" android:host="www.example.in" />

</intent-filter>

然后将这个数组设置为杂志属性。

有了ArrayList,您不必担心大小,它会急剧增加。

Supplements sup1 = new Supplements("sup1", 10.0);
Supplements sup2 = new Supplements("sup2", 20.0);
Supplements sup3 = new Supplements("sup3", 30.0);

依次类推,如果您有任何疑问,请随时发表评论。

相关问题