Eclipse changes my Code [message #1830783] |
Mon, 03 August 2020 19:07  |
Eclipse User |
|
|
|
I am teaching myself java by going thru the book Java and Eclipse for ComputerScience. I am working on an example in chapter five. I type in the code given in the book, but when I run it, certain lines and variables are changed by Eclipse. It appears the changed lines are easier to understand but it interferes with me following the example in the book. How do I stop Eclipse from changing my code?
|
|
|
|
Re: Eclipse changes my Code [message #1830811 is a reply to message #1830791] |
Tue, 04 August 2020 11:08   |
Eclipse User |
|
|
|
Yes, i have the following code directly from the book.
package array2d;
public class Array2D {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Basic test
int [][] test1 = new int[4][5];
test1[0][0] = 5;
test1[1][3] = 3;
System.out.println("The row length is " + test1.length);
System.out.println("The colunm length is " + test1[0].length);
//Search using counters
int z = 0;
// int[][] test2 = { { 3, 2, 6, 5 }, { 2, 7, 8, 1 }, { 4, 2, 7, 2 } };
int[][] test2 = { { 3, 2, 6, 5 }, { 2, 7 }, { 4, 2, 7, 2 }, { 7, 9, 2, 1, 6, 5 } };
//Counter search
for (int[] element : test2) {
for (int y = 0; y < element.length; y++ )
{
if(element[y] == 2)
{
z++;
}
}
}
System.out.println("The number 2 was found " + z + " times");
//Enhanced for-loop search
z=0;
for (int x[] : test2)
{
for(int y : x)
{
if (y == 7)
{
z++;
}
}
}
System.out.println("The number 7 was found " + z + " times");
}
}
Under the comment Counter Search the following 4 lines have the word 'element' in them. The original lines after counter search were:
for ( int x = 0; x< test2.length; x++) {
for (int y = 0;y< test2[x].length; y++) {
if ( test2[x][y] == 2) {
z++;
}
}
{
i understand the added lines are a shorter way of doing the double loop but i need the original code to follow the book. If you replace the lines after Counter search with these lines you'll have the original code.
thanks
|
|
|
|
Powered by
FUDForum. Page generated in 0.09594 seconds