-1

I need some help with this code, when i try to show all chars in Java with the "for loop" it's okay, java shows me all, when i do this with the "for each" i have only undefined symbols

char[] chars = new char[65535];
    for (int go = 0; go < chars.length; go++) { // THIS CODE IS OKAY!
        // for (int go : chars) { // **THIS CODE DOESN'T WORKS! WHY??** 
        chars[go] = (char) go; // Change int type to char
    }
    for (char owo : chars) { // Output characters
        System.out.println(owo);
    }
Ess
  • 3,350
  • 4
  • 12
  • 20
  • 3
    Your chars array doesn't contain anything. In the first example you use the counter of the loop to create/set all possible chars! Since the enhanced for loop doesn't have a counter this obviously won't work and just declaring the type as int shows a misunderstanding of what an enhanced for loop does. – OH GOD SPIDERS Mar 13 '17 at 13:16
  • *"when i try to show all chars in Java with the "for loop" it's okay, java shows me all"* I doubt that. At least with the code you've provided (your first code doesn't print anything and you're doing two different things). – Tom Mar 13 '17 at 13:32
  • `for (int go : chars)` is a little strange considering the base type of `chars` is not `int`. Aside from the logic flaw others have pointed out. – Lew Bloch Mar 13 '17 at 14:25

2 Answers2

1
for (int go : chars) {

Used when you are sure that your array is not empty

for (int go = 0; go < chars.length; go++) {

Used when you want to get the values of i, 0, 1, 2, ...n

EDIT

If that is required to use for (int go : chars) { you have to add another variable like this :

int i = 0;
for (int go : chars) {
    chars[i] = (char) i;
    i++;
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • So if i need to do this program with the only for-each loop, how is it? – Ess Mar 13 '17 at 13:31
  • if you want to use `for (int go : chars) {` you have to add another variable @Ess check my edit – Youcef LAIDANI Mar 13 '17 at 13:37
  • Why use `int go` when `chars` doesn't have `int` as its base type? – Lew Bloch Mar 13 '17 at 14:26
  • The for-each loop shown here is bad. It extracts `go` but never uses that variable within the loop. This tells us that that is the wrong approach. – Lew Bloch Mar 13 '17 at 14:28
  • @LewBloch in your first comment yes you are true, the second this is what the OP ask **So if i need to do this program with the only for-each loop, how is it?** if you can share another solution i will be happy LewBloch – Youcef LAIDANI Mar 13 '17 at 14:31
  • When you make a down-vote why you don't follow it with a comment please? – Youcef LAIDANI Mar 13 '17 at 14:33
  • Someone else here already provided that. And to heck with the homework assignment, using a for-each is contraindicated when you don't use the variable. Homework or no homework, it's just plain the wrong thing to do. – Lew Bloch Mar 13 '17 at 14:33
  • mmmm, ok @LewBloch thank u – Youcef LAIDANI Mar 13 '17 at 14:46
0

nothing in your array, the number 65535 is just the size of the array, it means chars[0] to chars[65534]

Lee
  • 27
  • 4