Best way to create an array of Strings in a called method

B

Bob Brown

Hi all,

I've been a programmer for yonks including with C++ and have recently
started using Java.

I know that when you pass classes as arguments to methods they are
references to the class. So this means that to return a completely
new instance of a class you must create an instance of the class
first. Is this correct?

Thus, if I want a method to return an array of Strings, then I must do
this by having something like an ArrayList returned. Thus I would
have

public void setStringArray(ArrayList tcStringArray) {
String lString=new String("one");
tcStringArray.add(lString);
....
}

public void dosomething() {
....
ArrayList lAL = new ArrayList();
setStringArray(lAL);
// convert lAL to an array of Strings now using toArray() but I'm
yet to work out how!
}

Given what I understand of references (and that Java doesn't use
pointers), this seems to be the only way you can create an array of
Strings in a called method. Am I correct? Is there a cleaner way to
arrange this? How does everybody like to write this?

Thankyou,

BB
 
C

Christophe Vanfleteren

Bob said:
Hi all,

I've been a programmer for yonks including with C++ and have recently
started using Java.

I know that when you pass classes as arguments to methods they are
references to the class. So this means that to return a completely
new instance of a class you must create an instance of the class
first. Is this correct?

You don't pass classes, you pass references to instances (an object) of a
particular class.
Thus, if I want a method to return an array of Strings, then I must do
this by having something like an ArrayList returned. Thus I would
have

public void setStringArray(ArrayList tcStringArray) {
String lString=new String("one");
tcStringArray.add(lString);
....
}

public void dosomething() {
....
ArrayList lAL = new ArrayList();
setStringArray(lAL);
// convert lAL to an array of Strings now using toArray() but I'm
yet to work out how!
}

Given what I understand of references (and that Java doesn't use
pointers), this seems to be the only way you can create an array of
Strings in a called method. Am I correct? Is there a cleaner way to
arrange this? How does everybody like to write this?

Not sure I understand you, but

public List getList() {
List l = new ArrayList();
l.add("one");//no need for "new String("one")
return l;
}

or using an array:

public String[] getArray() {
return new String[]{"one};
//or String[] sa = new String[1];
//sa[0] = "one";
//return sa;
}

public void doSomething() {
List l = getList();
//do stuff with list
}


Btw. you might consider posting beginner problems like these to c.l.j.help
instead.
 
R

Roedy Green

I know that when you pass classes as arguments to methods they are
references to the class. So this means that to return a completely
new instance of a class you must create an instance of the class
first. Is this correct?

no. See http://mindprod.com/jgloss/classforname.html
for ways to get a hold of the corresponding Class object.

Classes are singletons. However, identical classes loaded by different
class loaders are considered distinct classes.

You don't go creating new instances of the class Object. You get a
reference to the singleton.
 
R

Roedy Green

no. See http://mindprod.com/jgloss/classforname.html
for ways to get a hold of the corresponding Class object.

Classes are singletons. However, identical classes loaded by different
class loaders are considered distinct classes.

You don't go creating new instances of the class Object. You get a
reference to the singleton.

if you are a newbie, please ignore my answer. Using class objects is
fairly advanced Java.

Instead read http://mindprod.com/jgloss/reference.html
and chase links.
 
R

Roedy Green

If you want to return an array of String there are two ways to do it.

1. instantiate a new String array and populate it.
See http://mindprod.com/jgloss/gotchas.html#ARRAY

2. have the caller pass you an empty String[] array as a parameter.
Then you can either return void and let the caller get the values from
his own original reference to the array or return that array after
populating it with values.


See http://mindprod.com/jgloss/arraylist.html for an example of
technique 2.


// converting an ArrayList to an array
String[] predators = ( String [] ) al.toArray ( new String [al.size()]
);
 
B

Bob Brown

Christophe Vanfleteren said:
public List getList() {
List l = new ArrayList();
l.add("one");//no need for "new String("one")
return l;
}
Btw. you might consider posting beginner problems like these to c.l.j.help
instead.

Thanks Christophe. My question was asking how you handle returning an
array of Strings as a method argument. I will redirect my query to
the help list instead.

Cheers,

BB

Bob said:
Hi all,

I've been a programmer for yonks including with C++ and have recently
started using Java.

I know that when you pass classes as arguments to methods they are
references to the class. So this means that to return a completely
new instance of a class you must create an instance of the class
first. Is this correct?

You don't pass classes, you pass references to instances (an object) of a
particular class.
Thus, if I want a method to return an array of Strings, then I must do
this by having something like an ArrayList returned. Thus I would
have

public void setStringArray(ArrayList tcStringArray) {
String lString=new String("one");
tcStringArray.add(lString);
....
}

public void dosomething() {
....
ArrayList lAL = new ArrayList();
setStringArray(lAL);
// convert lAL to an array of Strings now using toArray() but I'm
yet to work out how!
}

Given what I understand of references (and that Java doesn't use
pointers), this seems to be the only way you can create an array of
Strings in a called method. Am I correct? Is there a cleaner way to
arrange this? How does everybody like to write this?

Not sure I understand you, but

public List getList() {
List l = new ArrayList();
l.add("one");//no need for "new String("one")
return l;
}

or using an array:

public String[] getArray() {
return new String[]{"one};
//or String[] sa = new String[1];
//sa[0] = "one";
//return sa;
}

public void doSomething() {
List l = getList();
//do stuff with list
}


Btw. you might consider posting beginner problems like these to c.l.j.help
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top