How do these Java examples work? (existence and bubble sort)












1















  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?










share|improve this question





























    1















      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?










    share|improve this question



























      1












      1








      1








        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?










      share|improve this question
















        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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 4:02









      Cœur

      18.2k9108148




      18.2k9108148










      asked Aug 10 '17 at 10:33









      JustMatthewJustMatthew

      123111




      123111
























          4 Answers
          4






          active

          oldest

          votes


















          3














          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]






          share|improve this answer

































            2















            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.






            share|improve this answer

































              1















              1. 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.


              2. 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.







              share|improve this answer































                1














                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]=4and sorozat[j+1]=2 after that 3 lines the result would be sorozat[j]=2and sorozat[j+1]=4






                share|improve this answer
























                • Thanks for the quick answers!! :)

                  – JustMatthew
                  Aug 10 '17 at 11:10











                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
                });


                }
                });














                draft saved

                draft discarded


















                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









                3














                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]






                share|improve this answer






























                  3














                  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]






                  share|improve this answer




























                    3












                    3








                    3







                    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]






                    share|improve this answer















                    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]







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 10 '17 at 10:41

























                    answered Aug 10 '17 at 10:36









                    ayushayush

                    10.8k84586




                    10.8k84586

























                        2















                        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.






                        share|improve this answer






























                          2















                          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.






                          share|improve this answer




























                            2












                            2








                            2








                            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.






                            share|improve this answer
















                            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.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 10 '17 at 10:51

























                            answered Aug 10 '17 at 10:38









                            cнŝdkcнŝdk

                            23.7k63052




                            23.7k63052























                                1















                                1. 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.


                                2. 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.







                                share|improve this answer




























                                  1















                                  1. 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.


                                  2. 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.







                                  share|improve this answer


























                                    1












                                    1








                                    1








                                    1. 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.


                                    2. 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.







                                    share|improve this answer














                                    1. 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.


                                    2. 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.








                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 10 '17 at 10:41









                                    GyrotankGyrotank

                                    145129




                                    145129























                                        1














                                        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]=4and sorozat[j+1]=2 after that 3 lines the result would be sorozat[j]=2and sorozat[j+1]=4






                                        share|improve this answer
























                                        • Thanks for the quick answers!! :)

                                          – JustMatthew
                                          Aug 10 '17 at 11:10
















                                        1














                                        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]=4and sorozat[j+1]=2 after that 3 lines the result would be sorozat[j]=2and sorozat[j+1]=4






                                        share|improve this answer
























                                        • Thanks for the quick answers!! :)

                                          – JustMatthew
                                          Aug 10 '17 at 11:10














                                        1












                                        1








                                        1







                                        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]=4and sorozat[j+1]=2 after that 3 lines the result would be sorozat[j]=2and sorozat[j+1]=4






                                        share|improve this answer













                                        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]=4and sorozat[j+1]=2 after that 3 lines the result would be sorozat[j]=2and sorozat[j+1]=4







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 10 '17 at 10:43









                                        Rodry37Rodry37

                                        112




                                        112













                                        • 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





                                        Thanks for the quick answers!! :)

                                        – JustMatthew
                                        Aug 10 '17 at 11:10


















                                        draft saved

                                        draft discarded




















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                        Alcedinidae

                                        Origin of the phrase “under your belt”?