create an array of long - java

I'm trying to create an array of long in java, but eclipse is showing me an error as shown below:
Below is my code:
How can I resolve this?
Can't i create a long size array in java?

Arrays of longs are fine: long[]. But all arrays are int-indexed. So just change long n to int n in the method parameter declaration, and you'll be all set.

For index you have to use int but not long

i need an array of 10^9 elements
You can create an array of one billion using an int value. Make n an int, and you can create an array with new long[n] Note: this will use 8 GB of heap.
Since you are building all the elements using a formula, you should be able to give all the values dynamically. i.e. create a component which is a "virtual" array which would save you having to generate an array of them all.
The reason I point this out is that the second loop is likely to take minutes at best if k is very small, or could easily take days.
BTW: I can't see all the code, but it appears you never need more than k+1 values which can be allocated once instead of n-k times.

n (the array capacity) has to be an integer not long

You have my sympathy. We go through this every time memory sizes increase. There is a strange expectation that this time array sizes will not need to increase in parallel with memory sizes.
Your best solution is probably to write your own class with long get(long index) and void set(long value, long index) methods. It could represent the data as a long[10][1000000000], and encapsulate the mapping between the natural long index and the actual pair of int indices.

Please note that array size is always equal to int size.
if you specify the array size more than 2147483647 you will get error.
long n;
long a[]=new long[n];
this will create error because long n exceeds 2147483647.
if int n then no error occurs.

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] arr = new long[n];//Just declare the size of array of int type , because we cannot declare the size of array as long type ,rather we can store the long type in it ,This code will fix the error .
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
for(long i:arr)
{
System.out.print(i+" ");
}
}
}

The index needs to be int so I convert long to int.
long n = sc.nextLong();
long[] arr = new long[(int)n];

Related

Finding common element in two arrays with best performing method

Implement a method that checks whether an integer is present in both integer array parameter 1 and integer array parameter 2 and prints the result of the search, with the best performance you can. The method parameters are: (1) the first integer array and (2) the second integer array of the same size as parameter 1 and (3) the integer to search for.
Note - Consider better performance to mean that a better performing method requires fewer general work steps to solve the problem with the same size of arrays. You may want to review the Java SE API page for java.util.Arrays
I was able to implement the solution but I am not sure if it the best-performing one because I am not using any java.util.Arrays methods as I am not sure which one to use necessarily to get me the best answer
public static void findCommonElements(int[] arr1, int[] arr2, int num){
for(int i = 0; i < arr1.length; i++){
for(int j = 0; j < arr2.length; j++){
if(arr1[i] == arr2[j] && arr1[i] == num){
System.out.println(num);
}
}
}
}
UPDATE:
I was able to update the code with following solution which completely removes for loop and implements binary for better performance
int[] arr1 = {7,8,5,1,2,3,6,7};
int[] arr2 = {9,8,6,4,1,2,4,5};
Arrays.sort(arr1);
Arrays.sort(arr2);
int index1 = Arrays.binarySearch(arr1, 5);
int index2 = Arrays.binarySearch(arr2, 5);
System.out.println(index1);
System.out.println(index2);
if(index1 < 0 || index2 < 0){
System.out.println("number not found in both arrays");
}
else{
System.out.println("number found in both arrays");
}
The problem description is a bit hard to follow, but by reference to the example code, I take this to be a fair rewording: "Write the best-performing method you can that takes two int arrays of the same length and a scalar int value i as parameters, and prints whether the value of i appears in both arrays."
Your first solution tests each pair of elements drawn one from the first array and the other from the second to determine whether they are equal to each other and to the target value. This is grossly inefficient for the problem as interpreted.
Your second solution sorts the arrays first, so as to be able to use a binary search to try to find the target element. This is better, but still inefficient. Although the binary searches are quite fast, the sorting required to prepare for them takes a lot more work than is saved by a single binary search.
Since it is sufficient to determine only whether the target value appears in both arrays, you can
scan each array for the target value, once, independently of the other.
skip the second scan if the first one does not find the target value
break early from each scan when the target value is found
The latter two are minor improvements, as they reduce only the minimum and average number of steps. The first, however, is a huge improvement, especially as array size increases, because for arrays of length n, then this requires a number of steps proportional to n in the worst case, whereas your first example code requires steps proportional to n2 in both the average and worst cases, and your second requires time proportional to n log n in the average and worst cases.
The implementation is left as the exercise it is intended to be. However, with respect to
I was able to implement the solution but I am not sure if it the
best-performing one because I am not using any java.util.Arrays
methods as I am not sure which one to use necessarily to get me the
best answer
, I don't think java.util.Arrays offers any method that particularly helps with this problem, especially given the orientation toward best possible performance.
You can use search the arrays using streams:
public static boolean findCommonElements(int[] arr1, int[] arr2, int num) {
return Arrays.stream(arr1).anyMatch(x -> x == num) &&
Arrays.stream(arr2).anyMatch(x -> x == num);
}
Similar method using linear search in arrays of Integer using Arrays.asList to convert arrays:
public static boolean findCommonElements(Integer[] arr1, Integer[] arr2, int num) {
return Arrays.asList(arr1).indexOf(num) > -1 &&
Arrays.asList(arr2).indexOf(num) > -1;
}

Iterate array if index range is in long

how to iterate through array/ list in Java. if index range is in long. As array/list only accept integer in position index.
eg
long arr[]=new long[5];
for(long i=0l;i<5l;i++)
arr[i]=i; // throw an error as arr[i] only accept integer
here arr[i] will throw an error because i is of type long and array takes input as integer in index location.
Can anyone help me out with this?
The size limit1 on arrays is Integer.MAX_VALUE so an array index that is a long makes no sense.
A List can have more than Integer.MAX_VALUE elements, but indexing it will be problematic because List::get takes an int argument.
So, you will struggle to use either arrays or lists (implemented using List) for really large data structures.
The solution ... if you really need one ... would be to implement your own List class with overload or alternative for operations that expose the size and indexing. It would not be trivial.
Another (possibly simpler) approach would be to represent your long[] as a long[][] and map the subscripts.
Finally, if you are using long as a subscript unnecessarily (i.e. the indexes don't need to go beyond Integer.MAX_VALUE), then:
long arr[] = new long[5];
for (long i = 0l; i < 5l; i++) {
arr[(int) i] = i;
}
1 - This is the theoretical maximum size. 1) You may get an OutOfMemoryError if you attempt to allocate an array that large. The JVM needs at least length * size(<type>) bytes of free contiguous storage to allocate an array of <type> of length length, where size(<type>) is the size of a primitive type or a reference. 2) In a 32 bit JVM, you are also limited by the address space dimensions. 3) In recent Hotspot JVMs, the largest array that you can allocate is actually Integer.MAX_VALUE - 5 elements: see Do Java arrays have a maximum size?
You don't need the index to be long even if your array consists of long's. Change it to
for(int i = 0; ....)
Two solutions:
Narrow the long to an integer by a cast arr[(int)i] = i;
Change the type of i to an integer and let the compiler widen it for you when it is assigned for(int i = 0; i < 5; i++) arr[i] = i;
As you cannot map all long values to a integer the compiler will not automatically narrow a variable. As you can map all integer values to a long the compiler will widen a variable automatically for you.

Confused of the error when creating a long array using a long variable as its size

My program needs to handle very big numbers as input, so I chose long. I have a error when I create an array of type long using a variable of type long as its size. Could someone please provide some insight about what went wrong here?
Error:
long[] Arr = new long[n];
^ //incompatible types: possible lossy conversion from long to int
Code:
private static long foo(long n, long m) {
if (n <= 1) return n;
long[] Arr = new long[n];
return 0;
}
Looks like n is of type Long. But the length of an Array can only be an int
If you need to handle very big numbers in your program, consider using BigInteger or BigDecimal types. These types have no theoretical limit and allocate a much memory as needed. So it's limited only the amount of available memory.

Why am I getting ArrayIndexOutOfBoundsException?

So I got this assignment while my teacher is away, and basically I have to make a student project. The student has a name, marks, and average. To calculate the average I decided to store the marks inside a int[] array.
public void addQuiz(int m)
{
int l = (marks.length);
marks[l] = m;
}
int[] marks = new int[8];
But when I run the function:
student.addQuiz(90);
I get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
Any help?
I'm not sure what the int[8] part does but it was in the tutorial I followed and it would give me a null pointer without it. There are eight marks in total so I just made it 8.
You can't dynamically add things to an array. You should use an array list instead.
Arraylist<Integer> marks = new ArrayList<Integer>();
then in your addQuiz method:
public void addQuiz(int m) {
marks.add(m)
}
You'll probably also need to change your method for calculating the average a bit, but that should be trivial and I'll leave it to you.
The error says: ArrayIndexOutOfBoundsException: 8
You have an array with 8 elements, indexed from 0 to 7 (inclusive). This array has a length of 8, and you are actually trying to access marks[8], when you only can go up to 7.
In Java, Array index starts from '0'. so, you cannot access the index of the array equal to the length of the array.if your arrays length is '8', then the last index of the array is '7' not '8'. if you are trying to access the illegal index of the array, then ArrayIndexOutOfBoundException is thrown. the code should be changed to
public void addQuiz(int m)
{
int l = (marks.length); //assuming marks is an array of length '8'
marks[l-1] = m; //index is 7 now
}
To calculate the average, you need to sum up the contents of the array (provided all the values are of int values) and then divided by the lenght of the array
int sum = 0;
int avg = 0;
for(int i=0; i<array.length;i++){
sum =sum+array[i];
}
avg = sum/array.length;
Hope this gives an idea
Use Arraylist<Integer> and then you can add to the list dynamically
There is not index in this array for this marks[l] = m;. use marks[l-1] = m;
You can call this for loop in main for getting marks 8 times.
for(int i=0;i<8;i++)
student.addQuiz(<marks you want to enter>, i);
You can define addQuiz function like below
public void addQuiz(int m, int arrayIndex)
{
marks[arrayIndex] = m;
}
int[] marks = new int[8]; //this will be as it is
These are minimal changes. You can make your code better by passing array marks to addQuiz as a parameter. But this will also work, its just that this is not the best way to write code.

Various ways of accessing an array

Today i did learn two ways of accessing the array, i would like to know the various ways of accessing an array element and the best practice of it. I am a student learning algorithm.
int [] arr;
long [] arr;
Advantages of long datatype declaration over int.
class ArrayApp{
public static void main(final String[] args){
long [] arr;
arr= new long[2];
int i;
arr[0]=112;
arr[1]=111;
**// Way one**
for(long l:arr)
{
System.out.println(l);
}
**// Way Two**
for(i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
There is no real difference between the ways here. Way one is just a syntax sugar for not to create an additional interation value.
The first way is preferable as it doesn't require an int i; variable and requires less printing. The second should be used when you don't want to iterate through the all array, but just a part of it.
There is no other ways to access the elements of array in java.
We've one declaration of an array:
long[] values = new long[100];
This creates an array for 100 long type values. Each value has an index (position) inside the array, the first index (an int value!) is 0, the last one is 99.
The traditional for loop increments an integer value to generate index position numbers. Those int values are used to access the long values of the array:
for (int index=0; index < values.length; index++) { // index is int
long value = values[index]; // value is long
// do something with value
}
The "enhanced" for loop simply hides this index and gives access to the long values with less code:
for (long value:values) {
// do something with value
}
That's all. If you don't need the index variable in your code, just use the enhanced for loop (second version in my answer)
The first way
for(long l:arr)
{
System.out.println(l);
}
The java runtime will autobox the l to a Long, as iterating this way requires that the class implement the Iterable interface. This way, also does not provide the current index of the array.
The second way
for(i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
Requires no casts to Long and you also have access to the current index. You also however have to be careful about iterating past the end of the array or accessing array elements below 0.
An int is shorter as a long. You can make an array of any object or primitive type. int[], Integer[], CustomClass[], whatever.
From java documentation:
int: The int data type is a 32-bit
signed two's complement integer. It
has a minimum value of -2,147,483,648
and a maximum value of 2,147,483,647
(inclusive). For integral values, this
data type is generally the default
choice unless there is a reason (like
the above) to choose something else.
This data type will most likely be
large enough for the numbers your
program will use, but if you need a
wider range of values, use long
instead.
long: The long data type is a 64-bit
signed two's complement integer. It
has a minimum value of
-9,223,372,036,854,775,808 and a maximum value of
9,223,372,036,854,775,807 (inclusive).
Use this data type when you need a
range of values wider than those
provided by int.

Categories