Able to insert more values than its length in an array in JAVA











up vote
0
down vote

favorite












I have created a Java program.I have defined array length to be 10 but when I am giving input 3,5,18,1,3,7,16,14,15,11,13,19 which contains more than 10 values the program is running fine. I am new to Java. Please help me understand how it is happening. My code is :



  package ds;

import java.util.Scanner;

public class Trade {

public static void maxprofit(Stringprice)
{
int prices= new int[price.length];
for(int i=0;i<price.length;i++)
{
prices[i]=Integer.parseInt(price[i]);
}
int min=prices[0];
int max=prices[prices.length-1];

for(int i=0;i<prices.length;i++)
{
if(i<prices.length-1-i)
{
if(prices[i]<min)
{
min=prices[i];
}
if(prices[prices.length-1-i]>max)
{
max=prices[prices.length-1-i];
}
}
else
break;

}

System.out.println(max-min);
}

public static void main(String args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);
String prices=new String[10];
String input=new String();
input=sc.next();
prices=input.split(",");


maxprofit(prices);

}

}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I have created a Java program.I have defined array length to be 10 but when I am giving input 3,5,18,1,3,7,16,14,15,11,13,19 which contains more than 10 values the program is running fine. I am new to Java. Please help me understand how it is happening. My code is :



      package ds;

    import java.util.Scanner;

    public class Trade {

    public static void maxprofit(Stringprice)
    {
    int prices= new int[price.length];
    for(int i=0;i<price.length;i++)
    {
    prices[i]=Integer.parseInt(price[i]);
    }
    int min=prices[0];
    int max=prices[prices.length-1];

    for(int i=0;i<prices.length;i++)
    {
    if(i<prices.length-1-i)
    {
    if(prices[i]<min)
    {
    min=prices[i];
    }
    if(prices[prices.length-1-i]>max)
    {
    max=prices[prices.length-1-i];
    }
    }
    else
    break;

    }

    System.out.println(max-min);
    }

    public static void main(String args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    String prices=new String[10];
    String input=new String();
    input=sc.next();
    prices=input.split(",");


    maxprofit(prices);

    }

    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have created a Java program.I have defined array length to be 10 but when I am giving input 3,5,18,1,3,7,16,14,15,11,13,19 which contains more than 10 values the program is running fine. I am new to Java. Please help me understand how it is happening. My code is :



        package ds;

      import java.util.Scanner;

      public class Trade {

      public static void maxprofit(Stringprice)
      {
      int prices= new int[price.length];
      for(int i=0;i<price.length;i++)
      {
      prices[i]=Integer.parseInt(price[i]);
      }
      int min=prices[0];
      int max=prices[prices.length-1];

      for(int i=0;i<prices.length;i++)
      {
      if(i<prices.length-1-i)
      {
      if(prices[i]<min)
      {
      min=prices[i];
      }
      if(prices[prices.length-1-i]>max)
      {
      max=prices[prices.length-1-i];
      }
      }
      else
      break;

      }

      System.out.println(max-min);
      }

      public static void main(String args) {
      // TODO Auto-generated method stub

      Scanner sc = new Scanner(System.in);
      String prices=new String[10];
      String input=new String();
      input=sc.next();
      prices=input.split(",");


      maxprofit(prices);

      }

      }









      share|improve this question













      I have created a Java program.I have defined array length to be 10 but when I am giving input 3,5,18,1,3,7,16,14,15,11,13,19 which contains more than 10 values the program is running fine. I am new to Java. Please help me understand how it is happening. My code is :



        package ds;

      import java.util.Scanner;

      public class Trade {

      public static void maxprofit(Stringprice)
      {
      int prices= new int[price.length];
      for(int i=0;i<price.length;i++)
      {
      prices[i]=Integer.parseInt(price[i]);
      }
      int min=prices[0];
      int max=prices[prices.length-1];

      for(int i=0;i<prices.length;i++)
      {
      if(i<prices.length-1-i)
      {
      if(prices[i]<min)
      {
      min=prices[i];
      }
      if(prices[prices.length-1-i]>max)
      {
      max=prices[prices.length-1-i];
      }
      }
      else
      break;

      }

      System.out.println(max-min);
      }

      public static void main(String args) {
      // TODO Auto-generated method stub

      Scanner sc = new Scanner(System.in);
      String prices=new String[10];
      String input=new String();
      input=sc.next();
      prices=input.split(",");


      maxprofit(prices);

      }

      }






      java arrays






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 6:46









      user3797489

      346




      346
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          input.split(",") returns a new array. So after you assign that array to your input variable, input references the new (larger) array instead of the original 10 elements array.



          Both of these initializations are pointless:



          String prices=new String[10];
          String input=new String();


          Instead, you can write:



          String input = sc.next();
          String prices = input.split(",");





          share|improve this answer





















          • are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
            – user3797489
            Nov 18 at 6:52






          • 1




            @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
            – Eran
            Nov 18 at 6:56










          • Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
            – user3797489
            Nov 18 at 6:59












          • @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
            – Eran
            Nov 18 at 7:04










          • @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
            – Maseed
            Nov 18 at 7:05











          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',
          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%2f53358546%2fable-to-insert-more-values-than-its-length-in-an-array-in-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          input.split(",") returns a new array. So after you assign that array to your input variable, input references the new (larger) array instead of the original 10 elements array.



          Both of these initializations are pointless:



          String prices=new String[10];
          String input=new String();


          Instead, you can write:



          String input = sc.next();
          String prices = input.split(",");





          share|improve this answer





















          • are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
            – user3797489
            Nov 18 at 6:52






          • 1




            @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
            – Eran
            Nov 18 at 6:56










          • Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
            – user3797489
            Nov 18 at 6:59












          • @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
            – Eran
            Nov 18 at 7:04










          • @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
            – Maseed
            Nov 18 at 7:05















          up vote
          2
          down vote



          accepted










          input.split(",") returns a new array. So after you assign that array to your input variable, input references the new (larger) array instead of the original 10 elements array.



          Both of these initializations are pointless:



          String prices=new String[10];
          String input=new String();


          Instead, you can write:



          String input = sc.next();
          String prices = input.split(",");





          share|improve this answer





















          • are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
            – user3797489
            Nov 18 at 6:52






          • 1




            @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
            – Eran
            Nov 18 at 6:56










          • Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
            – user3797489
            Nov 18 at 6:59












          • @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
            – Eran
            Nov 18 at 7:04










          • @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
            – Maseed
            Nov 18 at 7:05













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          input.split(",") returns a new array. So after you assign that array to your input variable, input references the new (larger) array instead of the original 10 elements array.



          Both of these initializations are pointless:



          String prices=new String[10];
          String input=new String();


          Instead, you can write:



          String input = sc.next();
          String prices = input.split(",");





          share|improve this answer












          input.split(",") returns a new array. So after you assign that array to your input variable, input references the new (larger) array instead of the original 10 elements array.



          Both of these initializations are pointless:



          String prices=new String[10];
          String input=new String();


          Instead, you can write:



          String input = sc.next();
          String prices = input.split(",");






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 6:48









          Eran

          273k35432516




          273k35432516












          • are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
            – user3797489
            Nov 18 at 6:52






          • 1




            @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
            – Eran
            Nov 18 at 6:56










          • Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
            – user3797489
            Nov 18 at 6:59












          • @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
            – Eran
            Nov 18 at 7:04










          • @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
            – Maseed
            Nov 18 at 7:05


















          • are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
            – user3797489
            Nov 18 at 6:52






          • 1




            @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
            – Eran
            Nov 18 at 6:56










          • Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
            – user3797489
            Nov 18 at 6:59












          • @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
            – Eran
            Nov 18 at 7:04










          • @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
            – Maseed
            Nov 18 at 7:05
















          are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
          – user3797489
          Nov 18 at 6:52




          are u saying that my original array size is increased? as I am using the same name "prices" to store the result of split function so how it is possible that it has created a new array with same name.
          – user3797489
          Nov 18 at 6:52




          1




          1




          @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
          – Eran
          Nov 18 at 6:56




          @user3797489 no. Your original array size is not increased. You are not using the original array. prices is a reference variable that references an array object. Before the prices = input.split(",") assignment, it references your original array (or length 10) and after the assignment it references a different array, which is larger. The original array is no longer referenced by any variable, which means it can be garbage collected.
          – Eran
          Nov 18 at 6:56












          Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
          – user3797489
          Nov 18 at 6:59






          Thanks for the explaination. I understood your point but I have a doubt that when try to initialize an array with a same name again in Java program then it gives error. So in that case why the reference is not changed. Suppose we define String prices=new String[10]; String prices=new String[11]; together in a java program then it throws error So in this case why the reference is not changing
          – user3797489
          Nov 18 at 6:59














          @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
          – Eran
          Nov 18 at 7:04




          @user3797489 that's because you declare the same variable twice in the same scope, which is not allowed. You can declare the variable once and assign to it multiple times. For example, you can write String prices=new String[10]; prices=new String[11]; instead.
          – Eran
          Nov 18 at 7:04












          @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
          – Maseed
          Nov 18 at 7:05




          @user3797489 Java doesn't allow to declare or initialize variables with the same names within the same scope.
          – Maseed
          Nov 18 at 7:05


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358546%2fable-to-insert-more-values-than-its-length-in-an-array-in-java%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno