Java Class : How do I set up a toString method for arrays?
I'm having a problem with the toString method, or my code could be wrong. Heres an example:
Code :
public class TestClass
{
private TestClass[] a;
private int[] b;
public TestClass(int...b) // Constructor
{
this.b
{
public TestClass[] concat(TestClass...b1)
{
TestClass[] temp = new TestClass[b1.length+b.length]
for(int i = 0;i<=temp.length;i++)
return TestClass(temp[i]); //Trying to return the elements of two arrays put together, I think this could be wrong
}
How would I do my toString?
I want to be able to do something like this :
(main)
Code :
TestClass a1 = new TestClass[1,2,3,4];
TestClass a2 = new TestClass[5,6,7,8,9];
TestClass a3; // Need help on this also, how do I overload my constructor to get an empty set of array?
a3 = a1.concat(a2);
System.out.print(a3); //Want the results " 1,2,3,4,5,6,7,8,9,10 "
Re: Java Class : How do I set up a toString method for arrays?
I'll walk you through a simple example of how to override the toString() method:
Let's say we have class named A, which had an int variable var which was set in the constructor:
Code java:
public class A {
int var = 0;
public A(int n) {
var = n;
}
}
Now let's say we want to override A's toString() method to print the value of var 5 times. Our new class would look like:
Code java:
public class A {
int var = 0;
public A(int n) {
var = n;
}
public String toString() {
String ret = var+"";
ret = ret+var;
ret = ret+var;
ret = ret+var;
ret = ret+var;
return ret;
}
}
Now, if we create a main that creates an A Object, and prints out an A Object to the console, our main would look like this:
Code java:
public class someClass {
public static void main(String[] args) {
A aObj = new A(5);
System.out.println(aObj);
}
}
and our output would look this like:
Now, that is how to override a toString() method. But what about printing out an array? Well, we can call a println on an array. Let's say we had a main which had an int array with the numbers 1 through 5. Then let's print out the array, both by just calling it, and with each int on its own line:
Code java:
public class someClass {
public static void main(String[] args) {
int[] array = new int[]{1,2,3,4,5};
System.out.println(array);
for(int i=0;i<array.length;i++)
System.out.println(array[i]);
}
}
And our output would be:
Quote:
[1, 2, 3, 4, 5]
1
2
3
4
5
With all of that information (and your knowledge of looping through an array which I see you have), see if you can figure some of it out. If you have some trouble, feel free to ask.
Re: Java Class : How do I set up a toString method for arrays?
Quote:
a3 = a1.concat(a2)
For that to work, concat() needs to return a TestClass object.
Re: Java Class : How do I set up a toString method for arrays?
@ aussiemcgr, much thanks, understand the toString now.
@Norm
I just notice examples in my book about that.
So now I have a little problem understanding the constructor. How would I set it up?
Code :
private int[] aList = new int[200]; //instance v
public TestClass(int[] a) // constructor
{
aList = a;
}
Would this allow to to create an object that takes in a list of array .
ex. :
Code :
TestClass t = new TestClass(1,2,3,4,5);
First time making a class while dealing with arrays, so I'm kind of loss.
Re: Java Class : How do I set up a toString method for arrays?
Have you coded the class and compiled it? What happened?
Have you tried the code in post#1?
Re: Java Class : How do I set up a toString method for arrays?
Wont let me compile :
Code :
public TestClass[] concat(TestClass...b1)
{
TestClass[] temp = new TestClass[b1.length+b.length]
for(int i = 0;i<=temp.length;i++)
return TestClass(temp);
Wont let me grab the length of b1, so i figured something is wrong with my constructor.
Re: Java Class : How do I set up a toString method for arrays?
Please post the full text of the compiler's error message.
Re: Java Class : How do I set up a toString method for arrays?
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
length cannot be resolved or is not a field
at TestClass.concat(TestClass.java:41)
at TestClassDriver.main(TestClassDriver.java:8)"
at TestClass.concat(TestClass.java:41) is :
Code :
TestClass[] temp = new TestClass[b1.length+b.length]
at TestClassDriver.main(TestClassDriver.java:8) is :
Re: Java Class : How do I set up a toString method for arrays?
What you posted is an execution time error, not a compiler error. There is no stack trace for compiler errors.
Can you post the full text of the compiler error message? The error messages I get include the source line and a ^ under where the error is located:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
Re: Java Class : How do I set up a toString method for arrays?
Hmm. Using eclipse, and on the line with the x it just says : "length cannot be resolved or is not a field"
Re: Java Class : How do I set up a toString method for arrays?
Sorry, you need to use the javac compiler to get good compiler error messages. The error messages I get compiling your code have nothing to do with the length attribute.