I have some doubts as to why the value of index
is not incrementing here.
The reason why I have declared my array like that is because I need to store n
natural numbers where (1 ≤ n ≤ 1012), so numbers are large which is why I have taken an array of type long
, but then I get an error that I cannot put any long
value in the group, which is why I cast it to int
. Is there any way to declare an array for this type of condition, as I want a large number of indexes, and I can not put a long number in the [ ]
.
hope you guys understand my problem
CODE:
import java.util.Scanner;
class Error{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long array[] = new long[(int) n];
long index = 0;
for (int j = 1; j <= n; j++) {
if (j % 2 != 0) {//odd
array[(int) index++] = j;
System.out.print(" " + array[(int) --index]);
System.out.print(index);// index value -> always 0 why??
System.out.print(j);
}
}
System.out.println();
}
}
OUTPUT:
Unix-Box ~/Desktop$ javac Error.java
Unix-Box ~/Desktop$ java Error
10
101 303 505 707 909
Unix-Box ~/Desktop$
the middle value is of index
and it is always 0
what i shout it to be like
10
101 313 525 737 949
Unix-Box ~/Desktop$