英文:
How to pass an arraylist to the Constructor
问题
我是Java初学者,我正在尝试编写一个介绍自己的程序。以下是我的脚本。
class Description
{
// 实例变量
private String name, national, dob;
private ArrayList<String> hobbies = new ArrayList<String>();
public Description()
{
}
// 其他构造函数
public Description(String name, String national, String dob)
{
this.name = name;
this.national = national;
this.dob = dob;
}
// 访问器方法
private String getname()
{
return name;
}
private String getnational()
{
return national;
}
private String getdob()
{
return dob;
}
private ArrayList<String> getHobbies(){
return hobbies;
}
// 修改器方法
public void setHobbies(ArrayList<String> hobbies)
{
this.hobbies = hobbies;
}
public void printInfo()
{
System.out.println("My Information");
System.out.printf("Name: %s%n", getname());
System.out.printf("National: %s%n", getnational());
System.out.printf("Date of birth: %s%n", getdob());
System.out.printf("I have %d hobbies %n", hobbies.size());
System.out.printf(" 1: %s%n", hobbies.get(0));
System.out.printf(" 2: %s%n", hobbies.get(1));
}
}
主类:-
class Lab_1
{
public static void main (String [] args)
{
Description ds = new Description("Daniel ", "Singapore", "01-01-2020");
ArrayList<Description> hobbies = new ArrayList<Description> ();
hobbies.add("Studying");
hobbies.add("Coding");
ds.printInfo();
}
}
然而,当我尝试运行脚本时,我收到了error: incompatible types: String cannot be converted to Description
错误。请问有没有专家可以指导我如何解决这个问题。谢谢!我还需要创建另一个名为"wishes"的ArrayList,并包括4个愿望。当我仍然卡在"hobbies"时,我无法继续并处理其他ArrayList。
英文:
I'm a beginner at java and I am trying to do a program to introduce myself.
Below is my script.
class Description
{
// Instance variable
private String name, national, dob;
private ArrayList<String> hobbies = new ArrayList<String>();
public Description()
{
}
// Other Constructor
public Description(String name, String national, String dob)
{
this.name = name;
this.national = national;
this.dob = dob;
}
// Copy Constructor
// Accessor Method
private String getname()
{
return name;
}
private String getnational()
{
return national;
}
private String getdob()
{
return dob;
}
private ArrayList geHobbies(){
return hobbies;
}
// Mutator Method
public void setHobbies(ArrayList hobbies)
{
this.hobbies = hobbies;
}
public void printInfo()
{
System.out.println("My Information");
System.out.printf("Name: %s%n", getname());
System.out.printf("National: %s%n", getnational());
System.out.printf("Date of birth: %s%n", getdob());
System.out.printf("I have %d hobbies %n", hobbies.size());
System.out.printf(" 1: %s%n", hobbies.get(0));
System.out.printf(" 2: %s%n", hobbies.get(1));
}
}
Main class:-
class Lab_1
{
public static void main (String [] args)
{
Description ds = new Description("Daniel ", "Singapore", "01-01-2020");
ArrayList <Description> hobbies = new ArrayList <Description> ();
hobbies.add("Studying");
hobbies.add("Coding");
ds.printInfo();
}
}
However I received error: incompatible types: String cannot be converted to Description
when I tried to run the script. Can any expert out there guide me on how I can solve this issue. Thank you! I am also required to create another arraylist call wishes, and include 4 wishes. while I'm still stuck at hobbies, I can't proceed and work on the other arraylist.
答案1
得分: 0
因为你试图向ArrayList
中添加字符串,但是ArrayList
没有被声明为存储String
值。
将以下代码:
ArrayList <Description> hobbies = new ArrayList <>();
替换为:
ArrayList <String> hobbies = new ArrayList <>();
然后进行以下操作:
hobbies.add("Studying");
hobbies.add("Coding");
ds.setHobbies(hobbies);
英文:
You got the error because you are trying to add strings to the ArrayList
but the ArrayList
has not been declared to store String
values.
Replace
ArrayList <Description> hobbies = new ArrayList <> ();
with
ArrayList <String> hobbies = new ArrayList <> ();
and then do the following
hobbies.add("Studying");
hobbies.add("Coding");
ds.setHobbies(hobbies);
答案2
得分: 0
你正在向一个持有Description
对象的集合中添加一个String
对象("Studying"
)。似乎是将方块钉进圆孔。
错误信息String cannot be converted to Description
在这方面非常清楚。
原代码:
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
ArrayList <Description> hobbies = new ArrayList <Description> ();
hobbies.add("Studying");
应更改为:
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
ArrayList <Description> descriptions = new ArrayList <Description> ();
descriptions.add( ds ) ;
要向Description
对象添加一系列爱好,将列表传递给你编写的setHobbies
方法。
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
List< String > hobbies = List.of( "Studying" , "Coding" ) ;
ds.setHobbies( hobbies ) ;
ArrayList <Description> descriptions = new ArrayList <Description> ();
descriptions.add( ds ) ;
可以将代码进一步优化。
Description ds = new Description( "Ron " , "Singapore" , "01-01-2020" ) ;
ds.setHobbies( List.of( "Studying" , "Coding" ) ) ;
List < Description > descriptions = new ArrayList <> () ;
descriptions.add( ds ) ;
英文:
You are adding a String
object ("Studying"
) to a collection that holds Description
objects. Square peg, round hole.
The error message String cannot be converted to Description
is quite clear on this matter.
This:
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
ArrayList <Description> hobbies = new ArrayList <Description> ();
hobbies.add("Studying");
…should be:
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
ArrayList <Description> descriptions = new ArrayList <Description> ();
descriptions.add( ds ) ;
To add a list of hobbies to the Description
object, pass the list to the setHobbies
method you wrote.
Description ds = new Description("Ron ", "Singapore", "01-01-2020");
List< String > hobbies = List.of( "Studying" , "Coding" ) ;
ds.setHobbies( hobbies ) ;
ArrayList <Description> descriptions = new ArrayList <Description> ();
descriptions.add( ds ) ;
We can tighten up that code.
Description ds = new Description( "Ron " , "Singapore" , "01-01-2020" ) ;
ds.setHobbies( List.of( "Studying" , "Coding" ) ) ;
List < Description > descriptions = new ArrayList <> () ;
descriptions.add( ds ) ;
答案3
得分: 0
你正在将string
类型的兴趣爱好添加到Description
类型中。
ArrayList<Description> hobbies = new ArrayList<Description>();
将其更改为字符串,并使用Description
对象ds
设置这些兴趣爱好:
ArrayList<String> hobbies = new ArrayList<String>(); // 创建类型为String的ArrayList,而不是Description
hobbies.add("Studying");
hobbies.add("Coding");
ds.setHobbies(hobbies); // 使用描述对象设置兴趣爱好
ds.printInfo();
输出:
我的信息
姓名:Ron
国籍:新加坡
出生日期:01-01-2020
我有2个兴趣爱好
1:Studying
2:Coding
英文:
You are adding hobbies of string
type to Description
Type.
ArrayList <Description> hobbies = new ArrayList <Description> ();
Change it to String and then set these hobbies with the object ds of Description
ArrayList<String> hobbies=new ArrayList<String>(); // Create ArrayList of type String not Description
hobbies.add("Studying");
hobbies.add("Coding");
ds.setHobbies(hobbies); // set the hobbies with the description object
ds.printInfo();
OUTPUT:-
My Information
Name: Ron
National: Singapore
Date of birth: 01-01-2020
I have 2 hobbies
1: Studying
2: Coding
答案4
得分: 0
你的兴趣爱好对象应该是类型为 ArrayList<String>
的。
将其传递到你的 Description
类构造函数中,可以像这样操作:
// 其他构造函数
public Description(String name, String national, String dob, ArrayList<String> hobbies)
{
this.name = name;
this.national = national;
this.dob = dob;
this.hobbies = hobbies;
}
以下是带有构造函数更改的完整类:
class Description
{
// 实例变量
private String name, national, dob;
private ArrayList<String> hobbies = new ArrayList<String>();
public Description()
{
}
// 其他构造函数
public Description(String name, String national, String dob, ArrayList<String> hobbies)
{
this.name = name;
this.national = national;
this.dob = dob;
this.hobbies = hobbies;
}
// 复制构造函数
// 访问器方法
private String getname()
{
return name;
}
private String getnational()
{
return national;
}
private String getdob()
{
return dob;
}
private ArrayList geHobbies(){
return hobbies;
}
// 修改器方法
public void setHobbies(ArrayList hobbies)
{
this.hobbies = hobbies;
}
public void printInfo()
{
System.out.println("My Information");
System.out.printf("Name: %s%n", getname());
System.out.printf("National: %s%n", getnational());
System.out.printf("Date of birth: %s%n", getdob());
System.out.printf("I have %d hobbies %n", hobbies.size());
System.out.printf(" 1: %s%n", hobbies.get(0));
System.out.printf(" 2: %s%n", hobbies.get(1));
}
}
以下是你的主方法:
public static void main(String []args){
ArrayList<String> hobbies = new ArrayList<String> ();
hobbies.add("Studying");
hobbies.add("Coding");
Description ds = new Description("Ron ", "Singapore", "01-01-2020", hobbies);
ds.printInfo();
}
英文:
Your hobbies object is suppose to be of type ArrayList<String>
To it pass to your Description class constructor you can do it like this
// Other Constructor
public Description(String name, String national, String dob,ArrayList<String> hobbies)
{
this.name = name;
this.national = national;
this.dob = dob;
this.hobbies=hobbies;
}
Here is your full class with constructor changes
class Description
{
// Instance variable
private String name, national, dob;
private ArrayList<String> hobbies = new ArrayList<String>();
public Description()
{
}
// Other Constructor
public Description(String name, String national, String dob,ArrayList<String> hobbies)
{
this.name = name;
this.national = national;
this.dob = dob;
this.hobbies=hobbies;
}
// Copy Constructor
// Accessor Method
private String getname()
{
return name;
}
private String getnational()
{
return national;
}
private String getdob()
{
return dob;
}
private ArrayList geHobbies(){
return hobbies;
}
// Mutator Method
public void setHobbies(ArrayList hobbies)
{
this.hobbies = hobbies;
}
public void printInfo()
{
System.out.println("My Information");
System.out.printf("Name: %s%n", getname());
System.out.printf("National: %s%n", getnational());
System.out.printf("Date of birth: %s%n", getdob());
System.out.printf("I have %d hobbies %n", hobbies.size());
System.out.printf(" 1: %s%n", hobbies.get(0));
System.out.printf(" 2: %s%n", hobbies.get(1));
}
}
Here is your main Method
public static void main(String []args){
ArrayList <String> hobbies = new ArrayList <String> ();
hobbies.add("Studying");
hobbies.add("Coding");
Description ds = new Description("Ron ", "Singapore", "01-01-2020", hobbies);
ds.printInfo();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论