Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:09:59+00:00 2026-06-12T16:09:59+00:00

I am trying to pass a string array as an argument to the constructor

  • 0

I am trying to pass a string array as an argument to the constructor of Wetland class;
I don’t understand how to add the elements of string array to the string array list.

import java.util.ArrayList;

public class Wetland {
    private String name;
    private ArrayList<String> species;
    public Wetland(String name, String[] speciesArr) {
        this.name = name;
        for (int i = 0; i < speciesArr.length; i++) {
            species.add(speciesArr[i]);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:10:00+00:00Added an answer on June 12, 2026 at 4:10 pm

    You already have built-in method for that: –

    List<String> species = Arrays.asList(speciesArr);
    

    NOTE: – You should use List<String> species not ArrayList<String> species.

    Arrays.asList returns a different ArrayList -> java.util.Arrays.ArrayList which cannot be typecasted to java.util.ArrayList.

    Then you would have to use addAll method, which is not so good. So just use List<String>

    NOTE: – The list returned by Arrays.asList is a fixed size list. If you want to add something to the list, you would need to create another list, and use addAll to add elements to it. So, then you would better go with the 2nd way as below: –

        String[] arr = new String[1];
        arr[0] = "rohit";
        List<String> newList = Arrays.asList(arr);
    
        // Will throw `UnsupportedOperationException
        // newList.add("jain"); // Can't do this.
    
        ArrayList<String> updatableList = new ArrayList<String>();
    
        updatableList.addAll(newList); 
    
        updatableList.add("jain"); // OK this is fine. 
    
        System.out.println(newList);       // Prints [rohit]
        System.out.println(updatableList); //Prints [rohit, jain]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1.I was trying to pass an array of string from 1 activity to another
Trying to pass a string argument to a function, which will then be used
I'm trying to pass a string argument to a target function in a process.
I was trying to pass a string object to System.out.printf(...) in Java, but I
I am trying to pass the value of a string from an array to
I'm trying to pass an array of string parameters to a C# ASP.NET web
I'm trying to use NUnit and pass in a string argument to the TestCase
I am trying to pass Array from one class to another I used detailViewController.nameArray
I'm trying to pass a String array from one activity to another but when
I'm trying to pass a string to chdir(). But I always seem to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.