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);
}
}
java arrays
add a comment |
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);
}
}
java arrays
add a comment |
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);
}
}
java arrays
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
java arrays
asked Nov 18 at 6:46
user3797489
346
346
add a comment |
add a comment |
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(",");
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.pricesis a reference variable that references an array object. Before theprices = 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 writeString 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
add a comment |
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(",");
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.pricesis a reference variable that references an array object. Before theprices = 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 writeString 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
add a comment |
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(",");
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.pricesis a reference variable that references an array object. Before theprices = 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 writeString 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
add a comment |
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(",");
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(",");
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.pricesis a reference variable that references an array object. Before theprices = 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 writeString 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
add a comment |
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.pricesis a reference variable that references an array object. Before theprices = 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 writeString 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
add a comment |
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%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
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