Can anyone explain the logic behind it [duplicate] - java

This question already has answers here:
post increment operator java
(3 answers)
Closed 8 years ago.
what would be the output of
1).
int j=0;
for (int i=0; i<100; i++) j=j++;
System.out.println(j);
I thought j=j++; will be equal to
int j2 = j;
j = j+1;
so I was expecting the out put would be 99. but when I compiled on eclipse output was 0.
2). and I could not understand what is the logic behind
((int)(char)(byte) -1)
When ran it on eclipse I got output as 65535.

j=j++;
is functionally equal to
int xxx = j;
j++;
j = xxx;
So the value of j stays the same. (Because the right side is evaluated first, including the increment, then the result is assigned to j)
As for ((int)(char)(byte) -1), a char is 16bit in size and unsigned, so the bit pattern of -1 results in 65535.

This is because the ++ works as follows:
a = 0;
a = a++; // a will get the assignment of the current value of a before the increment occurs so a = 0 in here
However, in the next case here:
a = 0;
a = ++a; // a will get the assignment of the incremented value of a so a = 1 in here

Related

Java multidimential array out of bounds [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I have been trying to fill my
int[][][] sudoku = new int[5][9][9];
array with
String[][] tmp = new String[21][21];
tmp[][] holds numbers like
005700020009600020
490060010140050030
For example this code works perfect and it gives me the number I want
System.out.println(Integer.valueOf(tmp[4][10]));
But this code
//sudoku1
b=0; c=0;
for (int i = 0; i < 6; i++) {
for (int j = 9; j < 18; j++) {
sudoku[1][b][c] = Integer.valueOf(tmp[i][j]);
c++;
}
b++;
}
throws "Index 9 out of bounds for length 9" error
You have c defined outside both loops, and added 1 each execution in the inner loop. The inner loop gets executed 6*9 times, so c can reach even the value 54, but it throws exception when it reaches 9 when trying to use sudoku[1][b][c], as the array indexes for the third element of the multiarray sudoku go from 0 to 8.

Why doesn't double equal works for the for loop condition whereas less than equals works in Java? [duplicate]

This question already has answers here:
Java for loop won't run...?
(4 answers)
Closed 1 year ago.
Why doesn't double equal work in Java?
int end = 10;
int start =0;
for(int i=start;i==end;i++) {
System.out.println(i);
Whereas the following works?
int end = 10;
int start =0;
for(int i=start;i<=end;i++) {
System.out.println(i);
The loop with the equal-operator will never be entered, because i==end (i.e. 0==10) is false on the first iteration of the loop, whereas i<=end (i.e. 0<=10) is not.
The for loop:
for(i=start; i<=end; ++i) {
}
is equivalent to:
i=start;
while(i<=end) {
++i;
}
I think you might be misunderstood on how a for loop works. Then syntax of a for loop is
for(A;B;C)
where B is a boolean.
The loop will continue to work until B is false.
if you have i == end, then it does not execute because i is not equal to end in the first place
when you have i <= end which works until i is greater than end.

why can't my java code read the condition? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
what I attend to do is when the array(from 0 to n-1) is given, to sum the array values with the index minus multiple of 3 start from the end.
for example, when 7 arrays are given, I want to sum a[0] a[2] a[3] a[5] a[6].
below is error part of the code I programmed.
int j = 0;
for(int i=0; i<n; i++){
j = i*3;
if(i!=n-j)
r += array[i];
}
my code can't read the condition and just sum all the array values.
I don't know why. can someone help me ?
Let's debug!
first time through the loop:
i = 0
j = i * 3 soo... 0
if (0 != 7 - 0) ... - it's not, of course, so i = 0 qualifies and is added.
next loop:
i = 1
j = 3
if (1 != 7 - 3) ... - it's not, of course, so i = 1 qualifies and is added.
Which you did not want.
Given that this is homework, this should be enough for you to take another step and figure out what you SHOULD be doing here. I will give some tips:
You can loop, counting backwards, just as well. You'd have to use i-- of course (decrement i by 1 every time), and you'd use int i = n or perhaps int i = n - 1 as initializing expression in your for loop.
That whole 'is it a 3rd factor from the top' part cannot be calculated using i at all, you'd need something separate for this. You can declare variables outside (you already did that, int j = 0, but you're not looking for 0 so much as 'the first index from the top I do not want'. Then you can use if to check if it is that index. If so, you don't want to add that number to your sum AND you want to update your j.
For tasks like that the modulo operator is usually your friend.
I tried it like this and it works:
int r = 0;
int[] array = new int[7];
for(int i = array.length; i >= 0; i--){
int numberOfIteration = array.length - i;
if(numberOfIteration % 3 > 0){
System.out.println("Add array[" + i + "]");
r += array[i];
}
}

What is printed as a result of executing the following code segment? And why does that occur?

What is printed as a result of executing the code segment that follows?
(I would normally just use the java program on my computer to do this. But, the program is updating, which will take another few hours.) Anyways, here is the code segment:
ArrayList<String> digits = new ArrayList<String>();
for (int k = 0; k <= 9; k++)
{
digits.add("" + k);
}
for (int k = 0; k <= 4; k++)
{
String d1 = digits.remove(k);
String d2 = digits.remove(k);
digits.add(k, d1 + "+" + d2);
}
System.out.println(digits);
I also want to know why the answer occurs going through this code by hand. I know I got an answer that was completely wrong, so I would also like to know why the answer that you guys get is the correct answer.
The output is
[0+1, 2+3, 4+5, 6+7, 8+9]
The numbers ranging from 0 to 9 are added to the array list by the first for-loop.
for (int k = 0; k <= 9; k++) { digits.add("" + k);}
Five times 2 numbers are removed, e.g. in the first iteration the numbers 0 and 1 are removed. Keep in mind that remove shifts subsequent elements to the left.
At the same iteration a string containing the two digits is inserted into the ArrayList at position k.
for (int k = 0; k <= 4; k++){
String d1 = digits.remove(k);
String d2 = digits.remove(k);
digits.add(k, d1 + "+" + d2);
}
Output should be something like this: [0+1, 2+3, 4+5, 6+7, 8+9]. Didn't compile though.
You can use this site to compile your code and run it.
https://www.compilejava.net/
It produces this:
[0+1, 2+3, 4+5, 6+7, 8+9]

Post/Pre-Increment confusion [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 6 years ago.
From what I understand, if I have a variable k = 5, and I do ++k, the new value of k = 6. If I do k++, the value remains 5 until k appears the second time in the program, which is when it is changed to 6. For e.g:
k = 5;
System.out.println(k++); //prints 5, but now that k has appeared the second time, its value is incremented to 6
System.out.println(k); //prints 6
However, in this code:
class test {
public static void main(String args[]){
for(int i =0; i<10; i++){
System.out.println(i);
}
int x = 0;
x++;
System.out.println(x);
}
}
Output:
0
1
2
3
4
5
6
7
8
9
1
In the loop, though the variable i appears for the 2nd time(in System.out.println(i)), its value remains at 0. But for x, when it appears for the second time( in System.out.println(x); ) its value is incremented.
Why? I'm confused about how post and pre-increment work.
For your code
for(int i =0; i<10; i++){
System.out.println(i);
}
firstly the variable i is initialized to 0, it then checks if it satisfies the condition i < 10 and print the variable and lastly increments the variable i.
This is how the for loop works.
In the second piece of code you have written, it increments the variable x and lastly prints the value of x.

Categories