Results 1 to 7 of 7
  1. #1
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default What did I do wrong?

    Hello, I tried to write a simple program, I thought it was simple at least, I didn't expect to get it wrong.

    Java Code:
    public class MultiDimensionalArrays {
    
    	public static void main(String[] args) {
    		
    		double[][] array1 = new double[20][20];
    		double[][] array2 = new double[20][20];
    		double[][] array3 = new double[20][20];
    		
    		// initialize array3 elements to 0
    		for (double[] i : array3) {
    			for (double j : i) {
    				j = 0.0;
    			}
    		}
    		
    		//print array 3
    		System.out.println("Printing array3, all elements should be 0.");
    				for (double[] i : array3) {
    			for (double j : i) {
    				System.out.print((int)j + " ");
    			}
    			System.out.println();
    		}
    		System.out.println();
    		
    		// initialize array1 with random numbers between 1 and 100
    		for (double[] i : array1) {
    			for (double j : i) {
    				j = (Math.random()*100)+1;
    			}
    		}
    		
    		// print array 1
    		System.out.println("Printing array1, all elements should be randomized between 1 and 100.");
    				for (double[] i : array1) {
    			for (double j : i) {
    				System.out.print((int)j + " ");
    			}
    			System.out.println();
    		}
    		System.out.println();
    		
    		// initialize array2 with random numbers between 1 and 100
    		for (double[] i : array2) {
    			for (double j : i) {
    				j = (Math.random()*100)+1;
    			}
    		}
    		
    		// print array 2
    		System.out.println("Printing array2, all elements should be randomized between 1 and 100.");
    		for (double[] i : array2) {
    			for (double j : i) {
    				System.out.print((int)j + " ");
    			}
    			System.out.println();
    		}
                    System.out.println();
    		
    		// add corresponding elements from array1 and 2 into array3
    		for (int i = 0; i < array1.length; i++) {
    			for (int j = 0; j < array1[0].length; j++) {
    				array3[i][j] += array1[i][j] + array2[i][j];
    			}
    		}
    		
    		// print array3
    		System.out.println("Printing array3, it should contain the sum of array1's and 2' corresponding elements.");
    		for (double[] i : array3) {
    			for (double j : i) {
    				System.out.print((int)j + " ");
    			}
    			System.out.println();
    		}
    		
    	}
    
    }
    It's very self explanatory but here's the output I get:

    Printing array3, all elements should be 0.
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    Printing array1, all elements should be randomized between 1 and 100.
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    Printing array2, all elements should be randomized between 1 and 100.
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    Printing array3, it should contain the sum of array1's and 2' corresponding elements.
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    The arrays (except array1) don't initialize. Also Eclipse warns me that the value of j is never used on lines 12, 29, and 46, but it IS used every time.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: What did I do wrong?

    Your code is doing the equivalent of:
    Java Code:
    int someInt = 0;
    int someOtherInt = someInt;
    
    someOtherInt = 1;
    and expecting 'someInt' to be changed to 1 as well.
    All your code is dong is the same as that, simply setting someOtherInt to a number and then throwing it away.

    You want something like:
    Java Code:
    for (int i = 0; i < myArray.length; i++) {
       myArray[i] = 1; // or whatever number.
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: What did I do wrong?

    Quote Originally Posted by Tolls View Post
    Your code is doing the equivalent of:
    Java Code:
    int someInt = 0;
    int someOtherInt = someInt;
    
    someOtherInt = 1;
    and expecting 'someInt' to be changed to 1 as well.
    All your code is dong is the same as that, simply setting someOtherInt to a number and then throwing it away.

    You want something like:
    Java Code:
    for (int i = 0; i < myArray.length; i++) {
       myArray[i] = 1; // or whatever number.
    }
    Thanks Tolls, how silly of me to use the array index alone without the array haha.

    I got rid of those confusing foreach loops and created functions, much better.

    Java Code:
    public class MultiDimensionalArrays {
    
    	public static void main(String[] args) {
    		
    		double[][] array1 = new double[20][20];
    		double[][] array2 = new double[20][20];
    		double[][] array3 = new double[20][20];
    		
    		// initialize array3 elements to 0
    		initArray(array3, true);
    		
    		//print array 3
    		printArray(array3, "Printing array3, all elements should be 0.");
    		
    		// initialize array1 with random numbers between 1 and 100
    		initArray(array1, false);
    		
    		// print array 1
    		printArray(array1,"Printing array1, all elements should be randomized " +
    				"between 1 and 100.");
    		
    		// initialize array2 with random numbers between 1 and 100
    		initArray(array2, false);
    		
    		// print array 2
    		printArray(array2,"Printing array2, all elements should be randomized " +
    				"between 1 and 100.");
    		
    		// add corresponding elements from array1 and 2 into array3
    		arrayAdd(array1,array2,array3);
    		
    		// print array3
    		printArray(array3,"Printing array3, it should contain the sum of " +
    				"array1's and 2' corresponding elements.");
    		
    	}
    	
    	public static void initArray(double array[][], boolean zero) {
    		for (int i = 0; i < array.length; i++) {
    			for (int j = 0; j < array[0].length; j++) {
    				if (zero == true)
    					array[i][j] = 0;
    				else
    					array[i][j] = (Math.random()*100+1);
    			}
    		}
    	}
    	
    	public static void printArray(double array[][], String message) {
    		System.out.print(message);
    		System.out.println();
    		for (int i = 0; i < array.length; i++) {
    			for (int j = 0; j < array[0].length; j++) {
    				System.out.print((int)array[i][j] + " ");
    			}
    			System.out.println();
    		}
    		System.out.println();
    	}
    	
    	public static void arrayAdd(double array1[][], double array2[][], double array3[][]) {
    		for (int i = 0; i < array1.length; i++) {
    			for (int j = 0; j < array1[0].length; j++) {
    				array3[i][j] += array1[i][j] + array2[i][j];
    			}
    		}
    	}
    
    }
    Printing array3, all elements should be 0.
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    Printing array1, all elements should be randomized between 1 and 100.
    85 69 90 47 28 78 81 78 50 55 98 4 17 96 4 60 88 100 29 14
    9 58 25 80 47 52 25 25 92 23 93 32 65 83 11 16 87 9 40 46
    60 69 97 88 57 41 41 68 83 66 29 80 76 11 95 93 28 2 43 26
    69 14 92 81 35 28 94 90 52 69 61 6 9 69 74 64 38 32 12 39
    54 89 38 97 85 86 7 48 98 63 93 6 9 35 43 63 43 11 39 87
    85 59 20 60 84 37 48 7 100 95 19 35 70 79 25 73 45 51 29 88
    2 72 94 87 51 84 93 48 91 71 97 46 8 75 54 81 28 82 99 39
    24 98 30 78 33 7 36 49 13 63 48 27 74 1 56 72 14 76 10 90
    3 93 30 5 83 49 78 60 18 11 40 53 15 59 98 59 64 34 74 78
    82 25 95 1 35 44 69 82 93 59 21 43 28 52 3 85 15 22 32 76
    71 31 15 62 14 65 15 57 22 67 96 32 100 63 6 60 10 25 10 16
    83 51 75 3 68 82 2 15 80 36 44 99 82 69 16 77 33 31 82 2
    39 38 47 34 11 42 2 71 74 38 76 44 27 7 28 77 28 66 66 76
    82 60 26 6 75 25 34 4 13 73 13 46 86 9 7 12 21 94 91 57
    22 4 42 16 88 4 55 65 88 51 61 64 27 31 91 29 87 38 27 71
    14 8 56 70 56 3 72 83 90 79 2 54 62 60 49 1 48 59 89 84
    9 98 64 93 34 24 97 31 79 95 17 76 22 86 42 12 83 55 54 57
    50 62 39 99 36 68 6 47 31 63 80 5 59 40 80 4 57 50 16 96
    60 45 13 68 82 36 62 17 78 97 11 72 43 30 63 33 65 54 30 73
    46 54 83 19 58 81 80 18 42 77 23 58 54 51 53 37 64 47 64 19

    Printing array2, all elements should be randomized between 1 and 100.
    92 59 96 45 90 18 94 41 17 64 1 77 67 98 70 29 60 10 71 96
    9 72 18 43 73 68 79 49 44 66 78 44 7 99 38 59 82 66 48 11
    4 55 47 79 26 5 68 65 51 45 80 42 50 72 67 46 31 33 32 91
    16 28 67 34 45 61 43 65 97 10 85 14 96 5 52 76 37 71 17 21
    75 99 71 14 63 84 74 21 72 95 74 46 26 4 97 7 10 51 20 15
    40 81 96 22 76 46 90 37 43 40 62 30 86 72 88 71 2 99 66 9
    28 62 31 96 33 84 42 29 50 44 36 18 12 42 4 31 98 29 84 12
    92 92 60 44 24 70 16 44 61 42 20 83 3 73 67 43 95 69 3 19
    45 13 91 24 97 48 96 31 90 5 91 71 29 62 19 58 13 48 70 26
    73 41 99 18 77 98 83 63 78 7 62 81 21 82 23 25 85 23 82 86
    70 44 6 63 24 72 62 44 91 72 17 93 40 21 27 91 42 61 30 78
    92 40 52 51 90 11 33 92 7 73 70 40 91 63 1 8 16 1 9 25
    58 18 7 48 92 27 73 3 55 88 64 60 51 80 59 81 9 94 40 27
    7 19 74 59 11 91 82 10 91 72 93 56 51 84 44 89 59 68 79 22
    23 84 11 52 57 14 47 37 73 96 55 47 29 49 45 85 87 55 70 90
    73 66 94 47 51 37 74 2 79 85 95 59 10 25 66 19 14 28 3 31
    25 1 86 87 37 9 27 22 97 89 14 51 51 62 3 1 99 75 99 82
    25 53 53 64 67 88 13 25 58 58 18 75 98 96 65 39 54 5 36 46
    80 72 21 67 25 99 8 81 57 25 20 77 62 94 55 47 91 94 75 32
    23 70 80 54 74 75 2 97 29 38 67 38 11 90 18 94 82 69 4 97

    Printing array3, it should contain the sum of array1's and 2' corresponding elements.
    178 128 186 93 118 96 176 119 68 120 99 82 84 195 75 89 149 111 101 110
    19 130 43 124 121 120 105 74 137 89 172 76 73 183 50 76 170 76 89 57
    65 125 144 167 83 46 109 133 134 111 109 123 126 84 163 140 60 36 75 117
    85 42 159 116 80 90 137 156 150 80 147 21 106 75 126 140 75 103 29 61
    129 188 110 111 148 170 81 69 171 159 167 53 35 40 141 71 54 63 59 102
    126 141 116 83 161 84 138 44 144 135 81 65 157 151 114 144 48 150 96 98
    31 135 125 184 85 168 135 78 141 116 133 65 20 118 58 113 127 111 184 52
    116 191 90 123 57 78 52 93 75 106 68 110 77 75 124 116 110 145 14 110
    48 106 121 29 180 97 175 91 109 17 132 124 44 121 118 117 77 82 145 104
    155 66 195 19 113 142 153 145 172 66 84 124 50 134 27 111 100 45 115 162
    142 76 21 126 38 137 77 102 113 139 114 126 141 85 33 151 52 87 41 94
    175 92 128 55 159 93 36 108 88 110 114 140 174 133 18 86 50 33 92 27
    97 56 54 83 103 70 76 74 130 127 140 105 78 88 88 158 37 160 107 103
    89 80 100 66 86 116 116 15 105 145 107 102 138 94 52 101 80 162 170 79
    46 89 54 69 145 19 103 102 161 148 117 111 57 80 136 115 175 94 97 161
    88 75 151 117 107 40 146 85 169 164 98 114 73 86 115 20 63 87 93 116
    35 100 150 181 72 33 125 53 176 184 32 128 73 148 46 13 182 131 154 140
    76 115 93 163 104 156 20 73 89 122 98 80 158 137 145 43 111 56 52 142
    141 117 35 135 108 136 71 98 136 122 31 149 106 125 119 81 157 149 105 105
    69 125 163 74 132 156 83 115 72 116 90 96 66 142 72 131 146 117 69 117

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: What did I do wrong?

    Note that it is completely unecessary to initialise ints to zero, since that is their default value (for attributes anyway).
    So 'array1', 'array2' and 'array3' are all populated with zeros when created.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: What did I do wrong?

    Quote Originally Posted by Astralogic View Post
    Thanks Tolls, how silly of me to use the array index alone without the array haha.
    You didn't use an index, you used a copy of a value in the array; you changed the copy but didn't change the original value. A construct like this:

    Java Code:
    double[] array= ...;
    for (double d : array)
       d= <some value>;
    is identical to:

    Java Code:
    double[] array= ...
    for (int secret_index; secret_index < array.length; secret_index++) {
       double d= array[secret_index];
       d= <some value>;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: What did I do wrong?

    They are already initialized to 0? Is that true of all numerical variables? I'm somewhat familiar with C++, so that's where the idea of needing to initialize everything comes from.

    Quote Originally Posted by JosAH View Post
    You didn't use an index, you used a copy of a value in the array; you changed the copy but didn't change the original value. A construct like this:

    Java Code:
    double[] array= ...;
    for (double d : array)
       d= <some value>;
    is identical to:

    Java Code:
    double[] array= ...
    for (int secret_index; secret_index < array.length; secret_index++) {
       double d= array[secret_index];
       d= <some value>;
    }
    kind regards,

    Jos
    I see, thanks. These for each loops are a little tricky for me to wrap my head around.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: What did I do wrong?

    Quote Originally Posted by Astralogic View Post
    They are already initialized to 0? Is that true of all numerical variables? I'm somewhat familiar with C++, so that's where the idea of needing to initialize everything comes from.
    I see, thanks. These for each loops are a little tricky for me to wrap my head around.
    Member variables are implicitly initialized to all bits zero; 'coincidentally' all bits zero represent 0, 0.0 and null. Local variables aren't implicitly initialized; you have to do it yourself and the compiler checks it.
    Some people call these for-each loops 'advanced loops' while all they do is hide the index or the Iterator from the programmer.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. What Am I doing Wrong?
    By siren1111 in forum New To Java
    Replies: 3
    Last Post: 12-20-2011, 02:01 AM
  3. where did i go wrong?
    By Sri Vidhya in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:11 PM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 11:45 AM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 08:35 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
  •