Reputation: 27
I have java.lang.OutOfMemoryError memory overflow issue. Hello, I have java.lang.OutOfMemoryError memory overflow issue, I am trying to write to write java code which takes a list of number couple and show me the number which couples or doubles here is the input data set line n1: represents the number of times the data set will be entered line n2: indicates the odd number of numbers to enter the third line n3: the odd number list containing a number that does not repeat
exemple:
3 ---->represents the number of times the data set will be entered
3 ----> indicates the odd number of numbers to enter
1 1234567884 1234567884 ----> liste of number
5
4 3 5 4 3
5
1 10 8 10 1
but i execute the code I have an overflow memory error. This is the code:
import java.util.ArrayList;
import java.util.Scanner;
public class myMain {
/**
* @function initialize, fonction qui initialise les valeurs d'un tableau
* @param tab: int, length: int
* @return void
* */
public static void initialize(int[] tab, int length) {
for(int i = 0; i<length; i++) {
tab[i] = 0;
}
}
public static void main(String[] args) {
ArrayList<Integer> a_pair_list = new ArrayList<Integer>();
ArrayList<Integer> a_impair_list = new ArrayList<Integer>();
int count[] = new int[Integer.MAX_VALUE]; // 100
int tab[] = new int[100];
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
for(int j = 0; j < N; j++) {
//on permet a l'utilisateur d'entrer des valeur séparer par des espaces
String a_num_c = sc.nextLine();
String[] list = sc.nextLine().split(" ");
initialize(tab, Integer.parseInt(a_num_c));
//on converti les chaine de nombre en entier
for(int k = 0; k < list.length; k++) {
tab[k] = Integer.parseInt(list[k]);
}
/* i : compteur, tmp : stock tmporairement la valeur
à un certain index du tableau tab[]*/
int i,tmp = 0;
initialize(count, tab.length);
for(i = 0; i < tab.length; i++){
tmp = tab[i];
count[tmp]++;
}
for(i=1; i < count.length; i++){
if(count[i] > 0 && count[i] == 1){
a_impair_list.add(i);
}else if(count[i] >= 2){
a_pair_list.add(i);
}
}
}
for(int i = 0; i < a_impair_list.size(); i++) {
System.out.println("Cas n°"+i+": "+a_impair_list.get(i));
}
}
}
Upvotes: 1
Views: 70
Reputation: 27
The problem is that if I put a lower value the first case or dataset would throw an error. only the last two datasets will be able to display correctly. what value did you set so that all datasets are executed without returning an error? cordially
Upvotes: 0
Reputation: 66
I did run your code in a quick scratch and got the following error:
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at myMain.main(scratch_17.java:19)
Line 19 is int count[] = new int[Integer.MAX_VALUE]; // 100
You should not initialise this array with Integer.MAX_VALUE
which is then an array with 2147483647 elements that are initialized with 0
Upvotes: 1