toString() method
Hi,
Please can someone help me figure out a simple problem...
I have 4 classes - one of which, say Class Test contains 3 related strings. I have a String toString() method in this class which returns getA + getB + getC, (getA etc. just returns the value as a string)
public String getA() {
return A;
} etc.
public String toString () {
return getA() + getB() + getC();
}
so that these can be accessed in another class as strings. This works OK,
public void setT (Test t) {
t.toString();
}
but I have another object which is an array of Test objects and I am confused how to set this toString() method and get method as I'm dealing with an array of objects.
public Test[] getTt () {
for(int k=0; k<4; k++)
return tt[k] = ?
}
If someone would please help me with this simple question I would be very grateful,
Thanks
[979 byte] By [
tjap] at [2007-9-26 1:30:20]

public Test[] getTt () {
for(int k=0; k<4; k++)
return tt[k] = ?
}
Is this method supposed to return the array, or is it a toString() method?
If you want to return a String, just use a StringBuffer to append each value of the array, and then return the String
public String getTt ()
{
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int k = 0 ; k < tt.length ; k++)
{
sb.append(" " + tt[k] + ", ");
}
sb.append("]");
return sb.toString();
}
This will return a string with all the array values
[1 , 2 , 3 .....]
You can fine-tune the method, but this should give you a basic idea.
If you want to return an array, just return it like you would return any variable
public Test[] getTt ()
{
return tt;
//-- where I assume tt is the variable that holds a reference to your local array.
}
HTH
-Dewang
Thanks dewangs -
this is what I'm getting confused about..
'cos I think that I need to both be able to return the array, so that it can be accessed in another class (the Tt array object and a string data member make up ClassB, and another class contains an array of type B) , but I also need to make sure that when these values are printed out, they return a string value.
In this method I think that I need to build up the strings.
> public Test[] getTt () {
> for(int k=0; k<4; k++)
> return tt[k] = ?
> }
>
> Is this method supposed to return the array, or is it
> a toString() method?
>
> If you want to return a String, just use a
> StringBuffer to append each value of the array, and
> then return the String
>
> > public String getTt ()
> {
> StringBuffer sb = new StringBuffer();
> sb.append("[");
> for(int k = 0 ; k < tt.length ; k++)
> {
> sb.append(" " + tt[k] + ", ");
> }
> sb.append("]");
> return sb.toString();
> }
>
> This will return a string with all the array values
> [1 , 2 , 3 .....]
> You can fine-tune the method, but this should give you
> a basic idea.
>
> If you want to return an array, just return it like
> you would return any variable
> > public Test[] getTt ()
> {
> return tt;
> //-- where I assume tt is the variable that holds a
> reference to your local array.
> }
>
>
> HTH
> -Dewang
tjap at 2007-6-29 1:28:16 >

To get the string value of an array as one long string you can do the following:
public String toString () {
String aString;
for(int k=0; k<tt.length; k++) {
aString += tt[k];
//if you want a space between each value then use the
//following:
aString += " ";
}
return aString;
}
I hope this helps.
Robert>
Robert -
thanks for your message, is it correct that what you were suggesting would concatenate a whole array? If I have an array, where each array slot is containing 4 data items, and these need to be formatted as a string together, but not with the other array slots, do you just require an inner loop?!
I think this oo stuff is getting me confused!
tjap at 2007-6-29 1:28:16 >

You are not the only one confused, I am not sure what you mean by your question. The code that I wrote works for an array with only one column but if you have an array with two column you would need another for loop to add the values to a string.
I am not sure how you can tell how many values are in the subsequent columns of an array that has two or more columns, I have not had to deal with such a thing, but I am sure that someone from this forum can shed some light on this.
Robert