Results 1 to 2 of 2
Thread: Override toString() Method
- 02-03-2012, 09:59 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
Override toString() Method
Hey all, long story short i'm string to override the default toString() method to do something else. The problem i'm running into (highlighted in red) is that I can't access my 2d ArrayList that I create in AsciiImage. My current code is as follows:
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class AsciiImage extends Image {
private int height;
private int width;
AsciiImage(int w, int h) {
height = h;
width = w;
ArrayList<ArrayList<Character>> characters = new ArrayList<ArrayList<Character>>();
for(int i=0; i<width; i++){
characters.add(new ArrayList<Character>());
for(int j=0; j<height; j++)
characters.get(i).add(new Character('c'));
}
}
public String toString(){
String rows = new String();
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
rows += (characters.get(i).get(j));
}
rows += "\n";
}
return rows;
}
}Last edited by Apparition78; 02-03-2012 at 10:01 PM.
-
Re: Override toString() Method
Perhaps you want to make characters a class variable by declaring it in the class, not in the class's constructor.
Similar Threads
-
Can we override static method?
By srinivasmallabathula in forum Advanced JavaReplies: 3Last Post: 06-24-2011, 02:29 PM -
Override class method
By Mekie in forum New To JavaReplies: 8Last Post: 11-01-2010, 06:26 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
How to override DefaultTreeCellRenderer not to use "toString"
By yannay in forum AWT / SwingReplies: 7Last Post: 03-15-2010, 04:05 PM -
How to override DefaultTreeCellRenderer not to use "toString"
By yannay in forum SWT / JFaceReplies: 0Last Post: 03-15-2010, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks