Results 1 to 2 of 2
  1. #1
    Apparition78 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Can we override static method?
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 06-24-2011, 02:29 PM
  2. Override class method
    By Mekie in forum New To Java
    Replies: 8
    Last Post: 11-01-2010, 06:26 AM
  3. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  4. Replies: 7
    Last Post: 03-15-2010, 04:05 PM
  5. Replies: 0
    Last Post: 03-15-2010, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •