如何将一个ArrayList传递给构造函数。

huangapple 未分类评论36阅读模式
英文:

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&lt;String&gt; hobbies = new ArrayList&lt;String&gt;();  
	
	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(&quot;My Information&quot;);
		System.out.printf(&quot;Name: %s%n&quot;, getname()); 
		System.out.printf(&quot;National: %s%n&quot;, getnational());
		System.out.printf(&quot;Date of birth: %s%n&quot;, getdob());
		
		System.out.printf(&quot;I have %d hobbies %n&quot;, hobbies.size());
		System.out.printf(&quot;  1: %s%n&quot;, hobbies.get(0));
		System.out.printf(&quot;  2: %s%n&quot;, hobbies.get(1));
	}
}
	

Main class:-

	class Lab_1 
	{
		
		public static void main (String [] args)
		{
	
			Description ds = new Description(&quot;Daniel &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 
			ArrayList &lt;Description&gt; hobbies = new ArrayList &lt;Description&gt; ();
			hobbies.add(&quot;Studying&quot;);
			hobbies.add(&quot;Coding&quot;);
		
			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 &lt;Description&gt; hobbies = new ArrayList &lt;&gt; ();

with

ArrayList &lt;String&gt; hobbies = new ArrayList &lt;&gt; ();

and then do the following

hobbies.add(&quot;Studying&quot;);
hobbies.add(&quot;Coding&quot;);
ds.setHobbies(hobbies);

答案2

得分: 0

你正在向一个持有Description对象的集合中添加一个String对象(&quot;Studying&quot;)。似乎是将方块钉进圆孔。

错误信息String cannot be converted to Description在这方面非常清楚。

原代码:

Description ds = new Description(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 
ArrayList &lt;Description&gt; hobbies = new ArrayList &lt;Description&gt; ();
hobbies.add(&quot;Studying&quot;);

应更改为:

Description ds = new Description(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 

ArrayList &lt;Description&gt; descriptions = new ArrayList &lt;Description&gt; ();
descriptions.add( ds ) ;

要向Description对象添加一系列爱好,将列表传递给你编写的setHobbies方法。

Description ds = new Description(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 
List&lt; String &gt; hobbies = List.of( &quot;Studying&quot; , &quot;Coding&quot; ) ;
ds.setHobbies( hobbies ) ;

ArrayList &lt;Description&gt; descriptions = new ArrayList &lt;Description&gt; ();
descriptions.add( ds ) ;

可以将代码进一步优化。

Description ds = new Description( &quot;Ron &quot; , &quot;Singapore&quot; ,  &quot;01-01-2020&quot; ) ; 
ds.setHobbies( List.of( &quot;Studying&quot; , &quot;Coding&quot; ) ) ;

List &lt; Description &gt; descriptions = new ArrayList &lt;&gt; () ;
descriptions.add( ds ) ;
英文:

You are adding a String object (&quot;Studying&quot;) 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(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 
    ArrayList &lt;Description&gt; hobbies = new ArrayList &lt;Description&gt; ();
    hobbies.add(&quot;Studying&quot;);

…should be:

    Description ds = new Description(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 

    ArrayList &lt;Description&gt; descriptions = new ArrayList &lt;Description&gt; ();
    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(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;); 
    List&lt; String &gt; hobbies = List.of( &quot;Studying&quot; , &quot;Coding&quot; ) ;
    ds.setHobbies( hobbies ) ;

    ArrayList &lt;Description&gt; descriptions = new ArrayList &lt;Description&gt; ();
    descriptions.add( ds ) ;

We can tighten up that code.

    Description ds = new Description( &quot;Ron &quot; , &quot;Singapore&quot; ,  &quot;01-01-2020&quot; ) ; 
    ds.setHobbies( List.of( &quot;Studying&quot; , &quot;Coding&quot; ) ) ;

    List &lt; Description &gt; descriptions = new ArrayList &lt;&gt; () ;
    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 &lt;Description&gt; hobbies = new ArrayList &lt;Description&gt; ();

Change it to String and then set these hobbies with the object ds of Description

  ArrayList&lt;String&gt; hobbies=new ArrayList&lt;String&gt;(); // Create ArrayList of type String not Description
  hobbies.add(&quot;Studying&quot;);
  hobbies.add(&quot;Coding&quot;);
  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&lt;String&gt; 的。

将其传递到你的 Description 类构造函数中,可以像这样操作:

// 其他构造函数
public Description(String name, String national, String dob, ArrayList&lt;String&gt; hobbies)
{
    this.name = name; 
    this.national = national;
    this.dob = dob;
    this.hobbies = hobbies;
}

以下是带有构造函数更改的完整类:

class Description 
{ 
    // 实例变量 
    private String name, national, dob;
    private ArrayList&lt;String&gt; hobbies = new ArrayList&lt;String&gt;();  

    public Description()
    {

    }

    // 其他构造函数
    public Description(String name, String national, String dob, ArrayList&lt;String&gt; 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&lt;String&gt; hobbies = new ArrayList&lt;String&gt; ();
    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&lt;String&gt;
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&lt;String&gt; 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&lt;String&gt; hobbies = new ArrayList&lt;String&gt;();  

public Description()
{

}

// Other Constructor 
public Description(String name, String national, String dob,ArrayList&lt;String&gt; 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(&quot;My Information&quot;);
    System.out.printf(&quot;Name: %s%n&quot;, getname()); 
    System.out.printf(&quot;National: %s%n&quot;, getnational());
    System.out.printf(&quot;Date of birth: %s%n&quot;, getdob());

    System.out.printf(&quot;I have %d hobbies %n&quot;, hobbies.size());
    System.out.printf(&quot;  1: %s%n&quot;, hobbies.get(0));
    System.out.printf(&quot;  2: %s%n&quot;, hobbies.get(1));
}
}

Here is your main Method


public static void main(String []args){
        
        ArrayList &lt;String&gt; hobbies = new ArrayList &lt;String&gt; ();
        hobbies.add(&quot;Studying&quot;);
        hobbies.add(&quot;Coding&quot;);
        Description ds = new Description(&quot;Ron &quot;, &quot;Singapore&quot;,  &quot;01-01-2020&quot;, hobbies); 
        ds.printInfo();
     }

huangapple
  • 本文由 发表于 2020年4月4日 23:25:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/61030379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定