This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/8/23

Images with char arrays

Kartoffelbrot Kartoffelbrot

2013/8/23

#
Hello Guys, I want to make an image out of characters. Therefore I started to write the constructor of my new class. My first idea was to use a two dimensional char array as parameter. But then I didn't know, how to went through it with two for loops, meaning I didn't knwo how to get the height and the width of the array. My second idea was to use 0 to infinite one dimensional char arrays as parameters, so that every line would be an own array:
    public Image(char[]... image){
        
    }
But then I had the problem, that I can't differentiate between the length of the image arrays and the number of parameter because you ask the same way for both, like image.length. Has anyone an idea to solve one of the problems or do I have to use width and length as parameters, too?
Kartoffelbrot Kartoffelbrot

2013/8/23

#
Another idea is to use make it this way, but I actually don't want to make it that way if it isn't necessary:
    public Image(String... image){  
          
    }  
davmac davmac

2013/8/23

#
meaning I didn't knwo how to get the height and the width of the array.
You can get the length of an array using .length, and a two-dimensional array in Java is an array of arrays, so for instance:
public Image(char [][] data)
{
   int height = data.length;
   if (height == 0) {
       return;
   }
   int width = data[0].length;
   ...
}
Kartoffelbrot Kartoffelbrot

2013/8/23

#
Thank you! Now I have a problem by filling these arrays:
new char[][]{{'/','\'},{'\','/'}};
By compiling I get the error "unclosed character literal" and the place after the first comma is marked, but why?
Gevater_Tod4711 Gevater_Tod4711

2013/8/23

#
A backslash initiates a escape-sequence. If you want to write a " in your string you can't just use " because that would end the string. So you use the escape-sequence \" which doesn't close the String but adds the character to your String. The same goes for the character ' . If you want to have a backslash in your String you have to use '\\' instead of '\'. But sometimes you have to be a bit carefulle with the backslashs because sometimes they are interpreted more than one time. E.g. when you want to use a backslash in the String method replace(String, String) you need to mask the backslash two times. So you need to use replace("\\\\", "new string"). That can be very iritating when it doesn't works because of this problem and you don't know how to fix it.
Kartoffelbrot Kartoffelbrot

2013/8/23

#
Thanks :D
You need to login to post a reply.