passing a string array

Hi there..
Can you please help me with my question..

how can I pass an array of strings into a constructor while creating an oblect..

in my the test class..i have create this array

string [] names = {"Bill", "John", "Ed", "Brian"};

and now I want to pass it as a parameter to this constructor

College c2 = new College ("ADMC", "London",                 , d1);
and then I will print c2.toString

so how can add pass the array in the space in my constructor
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should pass it in the same way that you've passed d1.  Simply treat the array of String's as you would any other object, the only difference is that in the constructor for the College class, you will need to specify the parameter as a String[].

Also note that it is String[], not string[]
just declare that the constructor will accept a string array.  You can have more than one constructor, if each accepts different parameters (has a different signature.)

public College(String[] data){


public College(String name, String location, String other){

swetAuthor Commented:
thanks alot for your help..
but I am still not sure what I have to do..

In the College class I declared the String array as the follow

private String major[] = new String [4];

and in the same Class I added this constructor

public College(String cn, String cLoc, String[] cMj, Director d) {

    cName = cn;
    cLocation = cLoc;
    major = cMj;
    dirc = d;

and in the test class I began th main method with declaring the array in different name

public static void main(String[] args) {
   String cMj1 = {"IT", "SWET", "CNET", "BIT"}; I have to do aloop or anything in order to print the results in the constructor?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

To print out the array you do need a loop, like:

for( int i = 0 ; i < cMj.length ; i++ )
  System.out.println( cMj[ i ] ) ;
swetAuthor Commented:

if I will declare the loop

String [] cMj1 = {"IT", "SWET", "CNET", "BIT"};

pass it

Director d1 = new Director("Jammie", "POBox 112", 5567783);
    College c2 = new College ("ADMC", "London", cMj1, d1);

I want to print c2 as the follow

at this situation,,how will I be able to print my array (cMj1) inside the constructor?

thanks alot
If you mean how can you get to print out the array in the System.out.println call, you will need to override the toString() method of Object, and use TimeYates's code to append to a String (or preferably StringBuffer) and return this from the method.

But here's another way to do it, in the College:

public String toString() {
  return "College - cMj = " + Arrays.asList(cmJ);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
swetAuthor Commented:

thank alot for your help..

I used the last way..In the college class the toString method looks like this

public String toString() {
    return "College name is: " + cName + "\n College location is: " + cLocation +
        "\n College - cMj = " + Arrays.asList(major)+ "\ncollege director is " + dirc;

but this an error..can't resolve symbole : variable Arrays

than;s in advance
you would need to import java.util.Arrays
swetAuthor Commented:

Now it works..

thanks very much..and sorry for annoying
> Now it works..

Can you accept the comment (or comments) that helped you solve the problem, and award them a grade?  (I would suggest A grade, it makes no difference to the number of points it costs you, but it rewards the expert more) :-)

I would suggest that grim_toasters comment was the one that helped you solve this :-)

If you feel that other people deserve points too, there is a "Split Points" link above the "Post a Comment" box, where you can allocate points to more than one expert (min 20 points per expert)


> and sorry for annoying

You haven't been annoying :-)  All the experts here are here to help, and now anyone else with the same problem should be able to find this answer and solve it for themselves too in the future :-)  The beauty of Experts Exchange...  By asking this question, you have probably helped someone else later on :-)

Good luck!

> If you feel that other people deserve points too

I am not saying you should split the points ;-)

(I know for one I don't deserve any)

I was mostly posting this bit of info, so that you know about it next time you need to ask a question :-)

Good luck again!

swetAuthor Commented:

Sorry..but I'm know in here and I don't know anything about points issu?

could you please explaint it to me more?

and thanks Mr.Tim for your words :)
Basically, you ask a question for a certain number of points...let's say 100

No matter what, it costs you 100 points for this question.

If you get a good answer or a lot of help, then you can give it an A grade.  This means that the person who answers gets 400 (100 * 4) Expert Points

A "B" grade means that they helped, but didn't really solve your problem, and the expert would get 300 points (100 * 3)

A "C" grade means that they didn't help at all (and you should really consider asking for the question to be removed at

"Expert Points" can't be used for asking questions, but they are cool, as the more points you have, the higher up the rankings you go (like the Expert "objects" has over 1M Expert Points!!)

Also, if you answer 5000 points of Expert Points each month, you get a free Premium Account for that month (so you have more points for questions) :-)

Basically, if you ever have any problems with the site, or the experts, or the other users, then posting a question on

Should get you the answer you need (the site Moderators are constantly looking at that area of the site, and answering questions)

Hope this helps!

Welcome to Experts Exchange! :-)

Good luck with your project!

swetAuthor Commented:

now I only have 5 points..and I can't post a new question..

what can I do to increase my points? I really have an urgent question..

thanks alot for your explanation :)
You have 3 options...

1) Wait a few days, till the number of points you have increases (you get a few points a day...not sure how many)
2) Purchase a Premium account from
3) Answer 5000 points worth of questions, and wait till the 1st December :-(

I don't think there is any other way :-(

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.