How do these Java examples work? (existence and bubble sort)
static void eldontes() {
System.out.println("--- Choosing ---");
int sorozat = new int{1, -1, 3, 5};
boolean exists = false;
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
System.out.println("There's negative: " + exists);
First of all, what's !exists in the for loop doing? !exist should mean that the boolean value is now true, shouldn't it?. So increment i utnil sorozat.length AND exists = true ? What does it mean?
Then here's this example:
static void buborekRendezes() {
System.out.println("--- Bubble order ---");
int sorozat = new int{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
System.out.println(Arrays.toString(sorozat));
}
Starting from int tmp = sorozat[j];
I really don't know what's happening. What does the last three lines do? Why is the tmp on the right side at the end of the code?
java for-loop boolean condition
add a comment |
static void eldontes() {
System.out.println("--- Choosing ---");
int sorozat = new int{1, -1, 3, 5};
boolean exists = false;
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
System.out.println("There's negative: " + exists);
First of all, what's !exists in the for loop doing? !exist should mean that the boolean value is now true, shouldn't it?. So increment i utnil sorozat.length AND exists = true ? What does it mean?
Then here's this example:
static void buborekRendezes() {
System.out.println("--- Bubble order ---");
int sorozat = new int{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
System.out.println(Arrays.toString(sorozat));
}
Starting from int tmp = sorozat[j];
I really don't know what's happening. What does the last three lines do? Why is the tmp on the right side at the end of the code?
java for-loop boolean condition
add a comment |
static void eldontes() {
System.out.println("--- Choosing ---");
int sorozat = new int{1, -1, 3, 5};
boolean exists = false;
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
System.out.println("There's negative: " + exists);
First of all, what's !exists in the for loop doing? !exist should mean that the boolean value is now true, shouldn't it?. So increment i utnil sorozat.length AND exists = true ? What does it mean?
Then here's this example:
static void buborekRendezes() {
System.out.println("--- Bubble order ---");
int sorozat = new int{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
System.out.println(Arrays.toString(sorozat));
}
Starting from int tmp = sorozat[j];
I really don't know what's happening. What does the last three lines do? Why is the tmp on the right side at the end of the code?
java for-loop boolean condition
static void eldontes() {
System.out.println("--- Choosing ---");
int sorozat = new int{1, -1, 3, 5};
boolean exists = false;
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
System.out.println("There's negative: " + exists);
First of all, what's !exists in the for loop doing? !exist should mean that the boolean value is now true, shouldn't it?. So increment i utnil sorozat.length AND exists = true ? What does it mean?
Then here's this example:
static void buborekRendezes() {
System.out.println("--- Bubble order ---");
int sorozat = new int{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
System.out.println(Arrays.toString(sorozat));
}
Starting from int tmp = sorozat[j];
I really don't know what's happening. What does the last three lines do? Why is the tmp on the right side at the end of the code?
java for-loop boolean condition
java for-loop boolean condition
edited Nov 22 '18 at 4:02
Cœur
18.2k9108148
18.2k9108148
asked Aug 10 '17 at 10:33
JustMatthewJustMatthew
123111
123111
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
The !exists
is to make sure the loop breaks as soon as the first negative value is found..
As per your next code fragment
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
This is a basic working code for Bubble sort in java..Read the algorithm first and then it would be easier to understand the code.
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
These three lines are to swap the values present at sorozat[j]
and sorozat[j+1]
add a comment |
First of all, what's !exists in the for loop doing?
In fact !exists
means that the boolean variable exists
is false
.
!exists (is equivalent to) exists == false
And it's used here in the condition block of the for
loop, to exit the loop whenever the elem<0
is met, in other words whenever exists == true
.
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
So increment i utnil sorozat.length AND exists = true ? What does it mean?
No it means increment while i<sorozat.length
and exists == false
, whenever exists
is set to true
we will quit the loop.
add a comment |
You increment i not until but while i is less than sorozat.length AND !exists. Term !exists is equivalent to (exists == false). So your loop stops when you either reach the end of sorozat, or you find what you are looking for, that is, an element less than zero.
Here the values are exchanged between sorozat[j] and sorozat[j + 1]. In order to do this, the sorozat[j] value is stored inside tmp variable to avoid it being overwritten, then sorozat[j + 1] is put into sorozat[j], and finally initial sorozat[j] value taken from tmp is written into sorozat[j + 1]. Without tmp variable you would lose sorozat[j] value.
add a comment |
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
This is the basic algorythm to exchange the values of two variables.
Basically, is exchanging the values sorozat[j]
and sorozat[j+1]
using a temporal variable in the process (tmp).
So, if sorozat[j]=4
and sorozat[j+1]=2
after that 3 lines the result would be sorozat[j]=2
and sorozat[j+1]=4
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45611287%2fhow-do-these-java-examples-work-existence-and-bubble-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
The !exists
is to make sure the loop breaks as soon as the first negative value is found..
As per your next code fragment
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
This is a basic working code for Bubble sort in java..Read the algorithm first and then it would be easier to understand the code.
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
These three lines are to swap the values present at sorozat[j]
and sorozat[j+1]
add a comment |
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
The !exists
is to make sure the loop breaks as soon as the first negative value is found..
As per your next code fragment
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
This is a basic working code for Bubble sort in java..Read the algorithm first and then it would be easier to understand the code.
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
These three lines are to swap the values present at sorozat[j]
and sorozat[j+1]
add a comment |
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
The !exists
is to make sure the loop breaks as soon as the first negative value is found..
As per your next code fragment
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
This is a basic working code for Bubble sort in java..Read the algorithm first and then it would be easier to understand the code.
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
These three lines are to swap the values present at sorozat[j]
and sorozat[j+1]
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
The !exists
is to make sure the loop breaks as soon as the first negative value is found..
As per your next code fragment
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
This is a basic working code for Bubble sort in java..Read the algorithm first and then it would be easier to understand the code.
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
These three lines are to swap the values present at sorozat[j]
and sorozat[j+1]
edited Aug 10 '17 at 10:41
answered Aug 10 '17 at 10:36
ayushayush
10.8k84586
10.8k84586
add a comment |
add a comment |
First of all, what's !exists in the for loop doing?
In fact !exists
means that the boolean variable exists
is false
.
!exists (is equivalent to) exists == false
And it's used here in the condition block of the for
loop, to exit the loop whenever the elem<0
is met, in other words whenever exists == true
.
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
So increment i utnil sorozat.length AND exists = true ? What does it mean?
No it means increment while i<sorozat.length
and exists == false
, whenever exists
is set to true
we will quit the loop.
add a comment |
First of all, what's !exists in the for loop doing?
In fact !exists
means that the boolean variable exists
is false
.
!exists (is equivalent to) exists == false
And it's used here in the condition block of the for
loop, to exit the loop whenever the elem<0
is met, in other words whenever exists == true
.
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
So increment i utnil sorozat.length AND exists = true ? What does it mean?
No it means increment while i<sorozat.length
and exists == false
, whenever exists
is set to true
we will quit the loop.
add a comment |
First of all, what's !exists in the for loop doing?
In fact !exists
means that the boolean variable exists
is false
.
!exists (is equivalent to) exists == false
And it's used here in the condition block of the for
loop, to exit the loop whenever the elem<0
is met, in other words whenever exists == true
.
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
So increment i utnil sorozat.length AND exists = true ? What does it mean?
No it means increment while i<sorozat.length
and exists == false
, whenever exists
is set to true
we will quit the loop.
First of all, what's !exists in the for loop doing?
In fact !exists
means that the boolean variable exists
is false
.
!exists (is equivalent to) exists == false
And it's used here in the condition block of the for
loop, to exit the loop whenever the elem<0
is met, in other words whenever exists == true
.
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
So increment i utnil sorozat.length AND exists = true ? What does it mean?
No it means increment while i<sorozat.length
and exists == false
, whenever exists
is set to true
we will quit the loop.
edited Aug 10 '17 at 10:51
answered Aug 10 '17 at 10:38
cнŝdkcнŝdk
23.7k63052
23.7k63052
add a comment |
add a comment |
You increment i not until but while i is less than sorozat.length AND !exists. Term !exists is equivalent to (exists == false). So your loop stops when you either reach the end of sorozat, or you find what you are looking for, that is, an element less than zero.
Here the values are exchanged between sorozat[j] and sorozat[j + 1]. In order to do this, the sorozat[j] value is stored inside tmp variable to avoid it being overwritten, then sorozat[j + 1] is put into sorozat[j], and finally initial sorozat[j] value taken from tmp is written into sorozat[j + 1]. Without tmp variable you would lose sorozat[j] value.
add a comment |
You increment i not until but while i is less than sorozat.length AND !exists. Term !exists is equivalent to (exists == false). So your loop stops when you either reach the end of sorozat, or you find what you are looking for, that is, an element less than zero.
Here the values are exchanged between sorozat[j] and sorozat[j + 1]. In order to do this, the sorozat[j] value is stored inside tmp variable to avoid it being overwritten, then sorozat[j + 1] is put into sorozat[j], and finally initial sorozat[j] value taken from tmp is written into sorozat[j + 1]. Without tmp variable you would lose sorozat[j] value.
add a comment |
You increment i not until but while i is less than sorozat.length AND !exists. Term !exists is equivalent to (exists == false). So your loop stops when you either reach the end of sorozat, or you find what you are looking for, that is, an element less than zero.
Here the values are exchanged between sorozat[j] and sorozat[j + 1]. In order to do this, the sorozat[j] value is stored inside tmp variable to avoid it being overwritten, then sorozat[j + 1] is put into sorozat[j], and finally initial sorozat[j] value taken from tmp is written into sorozat[j + 1]. Without tmp variable you would lose sorozat[j] value.
You increment i not until but while i is less than sorozat.length AND !exists. Term !exists is equivalent to (exists == false). So your loop stops when you either reach the end of sorozat, or you find what you are looking for, that is, an element less than zero.
Here the values are exchanged between sorozat[j] and sorozat[j + 1]. In order to do this, the sorozat[j] value is stored inside tmp variable to avoid it being overwritten, then sorozat[j + 1] is put into sorozat[j], and finally initial sorozat[j] value taken from tmp is written into sorozat[j + 1]. Without tmp variable you would lose sorozat[j] value.
answered Aug 10 '17 at 10:41
GyrotankGyrotank
145129
145129
add a comment |
add a comment |
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
This is the basic algorythm to exchange the values of two variables.
Basically, is exchanging the values sorozat[j]
and sorozat[j+1]
using a temporal variable in the process (tmp).
So, if sorozat[j]=4
and sorozat[j+1]=2
after that 3 lines the result would be sorozat[j]=2
and sorozat[j+1]=4
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
add a comment |
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
This is the basic algorythm to exchange the values of two variables.
Basically, is exchanging the values sorozat[j]
and sorozat[j+1]
using a temporal variable in the process (tmp).
So, if sorozat[j]=4
and sorozat[j+1]=2
after that 3 lines the result would be sorozat[j]=2
and sorozat[j+1]=4
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
add a comment |
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
This is the basic algorythm to exchange the values of two variables.
Basically, is exchanging the values sorozat[j]
and sorozat[j+1]
using a temporal variable in the process (tmp).
So, if sorozat[j]=4
and sorozat[j+1]=2
after that 3 lines the result would be sorozat[j]=2
and sorozat[j+1]=4
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
This is the basic algorythm to exchange the values of two variables.
Basically, is exchanging the values sorozat[j]
and sorozat[j+1]
using a temporal variable in the process (tmp).
So, if sorozat[j]=4
and sorozat[j+1]=2
after that 3 lines the result would be sorozat[j]=2
and sorozat[j+1]=4
answered Aug 10 '17 at 10:43
Rodry37Rodry37
112
112
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
add a comment |
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
Thanks for the quick answers!! :)
– JustMatthew
Aug 10 '17 at 11:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45611287%2fhow-do-these-java-examples-work-existence-and-bubble-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown