Issue with java.lang.ArrayIndexOutOfBoundsException - java

I'm new in Java programming and I'm trying to write a method that orders an array in crescent or decrescent mode.
public static int[] orderArray(int[] v, boolean mode) {
int newArray[] = {};
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i); else newArray[i] = getMax(v, i);
return newArray;
}
So in another class I put:
int[] myArray = {5,3,4,1,6};
//Call the method
int[] newOrdArray = Vettori.orderArray(myArray, true);
System.out.println(Vettori.printArray(newOrdArray, ","));
There are some methods that I wrote before and I'm using to reach my objective.
I mean the methods: printArray, getMin and getMax
public static int getMin(int[] v, int pos) { //getMin
int minimum = 0;
if (v.length>0)
for (int i=pos;i<v.length;i++) {
if(i==pos) minimum=v[i];
else if(v[i]<=minimum) minimum=v[i];
}
return minimum;
}
public static int getMax(int[] v, int pos) { //getMax
int max = 0;
if (v.length>0)
for (int i=pos;i<v.length;i++) {
if(i==pos) max=v[i];
else if(v[i]>=max) max=v[i];
}
return max;
}
public static String printArray(int[] v, String separator) { //printArray
String stampa = "";
if (v.length>0) {
boolean insert_sep = true;
for (int i=0;i<v.length;i++) {
insert_sep = v[i]!=v.length;
if (insert_sep) stampa+=Integer.toString(v[i])+separator;
else stampa+=Integer.toString(v[i]);
}
stampa = stampa.substring(0, stampa.length()-1);
} else stampa = "Invalid array!";
return stampa;
}
When I compile the code and try to ordinate the array, the program throws the java.lang.ArrayIndexOutOfBoundsException exception. I'm trying to find the issue in my code, but I can't...
Here the output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Utili.Vettori.ordinaVettore(Vettori.java:335)
at mainProgram.main(mainProgram.java:24)

Initialize array in orderArray method before adding any elements:
int[] newArray = new int[v.length];

Your problem lies in the orderArray method where you have created an array of size 0 then attempted to access elements of it causing an ArrayIndexOutOfBoundsException.
Change line
int newArray[] = {};
to create a new integer array of the same size as your method parameter v.
int newArray[] = new int[v.length];
Your resulting method should look like this
public static int[] orderArray(int[] v, boolean mode) {
int newArray[] = new int[v.length];
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i); else newArray[i] = getMax(v, i);
return newArray;
}

found your problem
int newArray[] = {};
for (int i=0; i<v.length; i++) if (mode) newArray[i] = getMin(v, i);
else newArray[i] = getMax(v, i);
you are creating empty array, and then try to access fields which are not exist

The problem is simple, on your "orderArray(int[] v, boolean mode)" method, you did not initialize your new array, and thus you get ArrayOutOfBoundException.
Here, you have to make these changes, I've commented out the errorneous line and added a new line for the array initialization;
// int newArray[] = {};
int newArray[] = new int[v.length];
The complete corrected code is as below;
public class TestArrayOrder {
public static void main(String[] args) {
int[] myArray = { 5, 3, 4, 1, 6 };
// Call the method
int[] newOrdArray = orderArray(myArray, true);
System.out.println(printArray(newOrdArray, ","));
}
public static int[] orderArray(int[] v, boolean mode) {
// int newArray[] = {}; //you should initialize the array
int newArray[] = new int[v.length]; // array initialization
for (int i = 0; i < v.length; i++)
if (mode)
newArray[i] = getMin(v, i);
else
newArray[i] = getMax(v, i);
return newArray;
}
public static int getMin(int[] v, int pos) { // getMin
int minimum = 0;
if (v.length > 0)
for (int i = pos; i < v.length; i++) {
if (i == pos)
minimum = v[i];
else if (v[i] <= minimum)
minimum = v[i];
}
return minimum;
}
public static int getMax(int[] v, int pos) { // getMax
int max = 0;
if (v.length > 0)
for (int i = pos; i < v.length; i++) {
if (i == pos)
max = v[i];
else if (v[i] >= max)
max = v[i];
}
return max;
}
public static String printArray(int[] v, String separator) { // printArray
String stampa = "";
if (v.length > 0) {
boolean insert_sep = true;
for (int i = 0; i < v.length; i++) {
insert_sep = v[i] != v.length;
if (insert_sep)
stampa += Integer.toString(v[i]) + separator;
else
stampa += Integer.toString(v[i]);
}
stampa = stampa.substring(0, stampa.length() - 1);
} else
stampa = "Invalid array!";
return stampa;
}
}
Here is the output for the "true" mode call of your orderArray method;
1,1,1,1,6
And the output for "false" mode;
6,6,6,6,6
Hope that it helps.

Related

Facing issue in LeetCode Compiler but works in IDE

Code:
public class twoSumSolution {
public int findFirstlocation(int[] a, int target) {
for (int i = a.length - 1; i > 0; i--) {
int temp = a[i];
for (int j = 0; j < i; j++) {
if (a[j] + temp == target) {
return j;
}
}
}
return -1;
}
public int findSecondlocation(int[] a, int first, int target) {
int holdtemp = a[first];
for (int i = 0; i < a.length; i++) {
if (holdtemp + a[i] == target) {
return i;
}
}
return -1;
}
}
public class TwoSum {
public static void main(String[] args) {
int[] nums = new int[] {
11,
2,
15,
7
};
int target = 9;
twoSumSolution s = new twoSumSolution();
int firstindex = s.findFirstlocation(nums, target);
int secondindex = s.findSecondlocation(nums, firstindex, target);
System.out.println("[" + firstindex + "," + secondindex + "]");
}
}
Error in LeetCode Compiler:
Line 7: error: cannot find symbol [in __Driver__.java]
int[] ret = new Solution().twoSum(param_1, param_2);
^
symbol: class Solution
location: class __DriverSolution__
Index output works well in any IDE such as Netbeans or Eclipse , but fails in leetCode compiler can someone guide me?Error is at line 7
Leetcode has the following solution signature which you should not change, that means you don't need to define the main function. Each leetcode problem comes with a default solution signature which you need to fill out with your solution, if you change the signature itself it will give you error.
eg. below is the default signature for the two-sum problem for Java. Assemble your code inside this twoSum function to make it work. You can add your functions if you need as shown.
class Solution {
public int[] twoSum(int[] nums, int target) {
// TODO: fill this out.
}
// eg. functions you can put inside the solution class.
public int findFirstlocation(int[] a , int target)
{
for (int i=a.length-1 ; i>0 ; i--)
{
int temp = a[i];
for(int j=0 ; j<i;j++){
if (a[j]+temp ==target){
return j;
}
}
}
return -1;
}
public int findSecondlocation(int[] a , int first ,int target)
{
int holdtemp = a[first];
for(int i=0 ; i<a.length ; i++){
if (holdtemp + a[i]==target){
return i ;
}
}
return -1;
}
}

Finding the smallest integer that appears at least k times

You are given an array A of integers and an integer k. Implement an algorithm that determines, in linear time, the smallest integer that appears at least k times in A.
I have been struggling with this problem for awhile, coding in Java, I need to use a HashTable to find the smallest integer that appears at least k times, it also must be in linear time.
This is what I attempted but it does not pass any of the tests
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
if (k <= table.get(arr[i])) {
ans = Math.min(ans, arr[i]);
}
}else{
table.put(arr[i], 1);
}
}
return ans;
}
Here is the empty code with all of the test cases:
import java.io.*;
import java.util.*;
public class Lab5
{
/**
* Problem 1: Find the smallest integer that appears at least k times.
*/
private static int problem1(int[] arr, int k)
{
// Implement me!
return 0;
}
/**
* Problem 2: Find two distinct indices i and j such that A[i] = A[j] and |i - j| <= k.
*/
private static int[] problem2(int[] arr, int k)
{
// Implement me!
int i = -1;
int j = -1;
return new int[] { i, j };
}
// ---------------------------------------------------------------------
// Do not change any of the code below!
private static final int LabNo = 5;
private static final String quarter = "Fall 2020";
private static final Random rng = new Random(123456);
private static boolean testProblem1(int[][] testCase)
{
int[] arr = testCase[0];
int k = testCase[1][0];
int answer = problem1(arr.clone(), k);
Arrays.sort(arr);
for (int i = 0, j = 0; i < arr.length; i = j)
{
for (; j < arr.length && arr[i] == arr[j]; j++) { }
if (j - i >= k)
{
return answer == arr[i];
}
}
return false; // Will never happen.
}
private static boolean testProblem2(int[][] testCase)
{
int[] arr = testCase[0];
int k = testCase[1][0];
int[] answer = problem2(arr.clone(), k);
if (answer == null || answer.length != 2)
{
return false;
}
Arrays.sort(answer);
// Check answer
int i = answer[0];
int j = answer[1];
return i != j
&& j - i <= k
&& i >= 0
&& j < arr.length
&& arr[i] == arr[j];
}
public static void main(String args[])
{
System.out.println("CS 302 -- " + quarter + " -- Lab " + LabNo);
testProblems(1);
testProblems(2);
}
private static void testProblems(int prob)
{
int noOfLines = prob == 1 ? 100000 : 500000;
System.out.println("-- -- -- -- --");
System.out.println(noOfLines + " test cases for problem " + prob + ".");
boolean passedAll = true;
for (int i = 1; i <= noOfLines; i++)
{
int[][] testCase = null;
boolean passed = false;
boolean exce = false;
try
{
switch (prob)
{
case 1:
testCase = createProblem1(i);
passed = testProblem1(testCase);
break;
case 2:
testCase = createProblem2(i);
passed = testProblem2(testCase);
break;
}
}
catch (Exception ex)
{
passed = false;
exce = true;
}
if (!passed)
{
System.out.println("Test " + i + " failed!" + (exce ? " (Exception)" : ""));
passedAll = false;
break;
}
}
if (passedAll)
{
System.out.println("All test passed.");
}
}
private static int[][] createProblem1(int testNo)
{
int size = rng.nextInt(Math.min(1000, testNo)) + 5;
int[] numbers = getRandomNumbers(size, size);
Arrays.sort(numbers);
int maxK = 0;
for (int i = 0, j = 0; i < size; i = j)
{
for (; j < size && numbers[i] == numbers[j]; j++) { }
maxK = Math.max(maxK, j - i);
}
int k = rng.nextInt(maxK) + 1;
shuffle(numbers);
return new int[][] { numbers, new int[] { k } };
}
private static int[][] createProblem2(int testNo)
{
int size = rng.nextInt(Math.min(1000, testNo)) + 5;
int[] numbers = getRandomNumbers(size, size);
int i = rng.nextInt(size);
int j = rng.nextInt(size - 1);
if (i <= j) j++;
numbers[i] = numbers[j];
return new int[][] { numbers, new int[] { Math.abs(i - j) } };
}
private static void shuffle(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int rndInd = rng.nextInt(arr.length - i) + i;
int tmp = arr[i];
arr[i] = arr[rndInd];
arr[rndInd] = tmp;
}
}
private static int[] getRandomNumbers(int range, int size)
{
int numbers[] = new int[size];
for (int i = 0; i < size; i++)
{
numbers[i] = rng.nextInt(2 * range) - range;
}
return numbers;
}
}
private static int problem1(int[] arr, int k) {
// Implement me!
Map<Integer, Integer> table = new TreeMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
if (table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
} else {
table.put(arr[i], 1);
}
}
for (Map.Entry<Integer,Integer> entry : table.entrySet()) {
//As treemap is sorted, we return the first key with value >=k.
if(entry.getValue()>=k)
return entry.getKey();
}
//Not found
return -1;
}
As others have pointed out, there are a few mistakes. First, the line where you initialize ans,
int ans = 0;
You should initialize ans to Integer.MAX_VALUE so that when you find an integer that appears at least k times for the first time that ans gets set to that integer appropriately. Second, in your for loop, there's no reason to skip the first element while iterating the array so i should be initialized to 0 instead of 1. Also, in that same line, you want to iterate through the entire array, and in your loop's condition right now you have i < k when k is not the length of the array. The length of the array is denoted by arr.length so the condition should instead be i < arr.length. Third, in this line,
if (k < table.get(arr[i])){
where you are trying to check if an integer has occurred at least k times in the array so far while iterating through the array, the < operator should be changed to <= since the keyword here is at least k times, not "more than k times". Fourth, k should never change so you can get rid of this line of code,
k = table.get(arr[i]);
After applying all of those changes, your function should look like this:
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
if (k <= table.get(arr[i])) {
ans = Math.min(ans, arr[i]);
}
}else{
table.put(arr[i], 1);
}
}
return ans;
}
Pseudo code:
collect frequencies of each number in a Map<Integer, Integer> (number and its count)
set least to a large value
iterate over entries
ignore entry if its value is less than k
if entry key is less than current least, store it as least
return least
One line implementation:
private static int problem1(int[] arr, int k) {
return Arrays.stream(arr).boxed()
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() >= k)
.map(Map.Entry::getKey)
.reduce(MAX_VALUE, Math::min);
}
This was able to pass all the cases! Thank you to everyone who helped!!
private static int problem1(int[] arr, int k)
{
// Implement me!
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
int ans = Integer.MAX_VALUE;
for (int i=0; i < arr.length; i++) {
if(table.containsKey(arr[i])) {
table.put(arr[i], table.get(arr[i]) + 1);
}else{
table.put(arr[i], 1);
}
}
Set<Integer> keys = table.keySet();
for(int i : keys){
if(table.get(i) >= k){
ans = Math.min(ans,i);
}
}
if(ans != Integer.MAX_VALUE){
return ans;
}else{
return 0;
}
}

Check if array is sorted in java

I want to create a method that returns 0 if array not sorted
returns 1 if it is sorted in ascending order and returns -1 if its sorted in descending order
This is what I have done so far:
public static int isSorted(int[] intArray) {
int end = intArray.length - 1;
int val = 0;
for (int i = 1; i < end; i++) {
if (intArray[0] < intArray[i]) {
val = 1;
}
else if (intArray[0] > intArray[i]) {
val = -1;
}
}
return v;
}
}
This returns 1 if its ascending and -1 if its descending.
But if I create a random array it does not return 0.
The question is how to check if both conditions fail, i.e.,
if its not sorted at all.
You can use two additional variables to count:
public static int isSorted(int[] intArray) {
int end = intArray.length-1;
int counterAsc = 0;
int counterDesc = 0;
for (int i = 0; i < end; i++) {
if(intArray[i] < intArray[i+1]){
counterAsc++;
}
else if(intArray[i] > intArray[i+1]){
counterDesc++;
}
}
if(counterDesc == 0){
return 1;
}
else if(counterAsc == 0){
return -1;
}
else return 0;
}
Your comparison is just between first element and other elements in the array.
val = 0;
for(int i=0;i<=end;i++){
for(int j=0;j<end;j++){
if(intArray[j]>intArray[j+1])
val = 1;
}
}
return val;
Here you go
public class stackoverflow {
public static int isSorted(int[] intArray) {
boolean sortedAsc = true;
boolean sortedDesc = true;
boolean sameValues=true;
int result = 0;
for (int i = 0; i < intArray.length-1; i++)
{
if (intArray[i] > intArray[i+1]) {
sortedAsc=false;
sameValues=false;
}
if (intArray[i] < intArray[i+1]) {
sortedDesc=false;
sameValues=false;
}
}
if(sortedAsc) result= 1;
if(sortedDesc) result = -1;
if(sameValues) result = 2;
return result;
}
public static void main(String[] args) {
// TODO code application logic here
int array[] = new int[4];
array[0]=1;
array[1]=2;
array[2]=3;
array[3]=4;
System.out.println(isSorted(array));
}

I am having an undefined error when trying to call a method in a different class

I have 2 classes written in java here they are
public class MinHeapify{
public MinHeapify(String a[], int start, int end){
minHeapify(a,start,end);
}
public static void exchange(String a[],int i,int j) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int parent(int i) {
return (int) Math.floor((i - 1)/2);
}
public static int left(int i) {
return 2*i + 1;
}
public static int right(int i) {
return 2*(i+1);
}
public static void minHeapify(String a[], int start,int end) {
int l = left(start); int r = right(start);
int smallest=0;
if(l >= end){
smallest = (l < start)? l: start;
}
if(r >= end){
smallest = (r < smallest)? r: smallest;
}
if(smallest != start) {
exchange(a,start,smallest);
minHeapify(a,smallest,end);
}
}
}
import java.util.Scanner;
public class ReservationArray{
private String [] array = new String [50];
public ReservationArray(String [] arg){
int next = 0;
int next2 = 0;
while(arg[++next]!= null){
if(next != arg.length){
arg[next] = array[next2];
next = next++;
next2 = next2++;
}
else if(next == array.length && next!= arg.length){
String [] temp = new String[2*array.length+1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
} else{
if(next < (array.length/2)-1){
String [] temp = new String[1/2*array.length-1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
}
}
}
minHeapify(array, 0, array.length-1);
}
The error that I get when i try and compile it is " The method minHeapify(java.lang.String[], int, int) is undefined for the type ReservationArray" and I do not understand what that means, am I missing something?
MinHeapify mh = new MinHeapify();
mh.minHeapify(array, 0, array.length-1);
Since your method is static, you should call this way.
MinHeapify.(array, 0, array.length-1);
add the return type to it
public void MinHeapify(String a[], int start, int end)
and
MinHeapify.minHeapify(array, 0, array.length - 1);
The method minHeapify belongs to the MinHeapifyclass rather than ReservationArray, hence the error. As its static, you can use:
MinHeapify.minHeapify(array, 0, array.length - 1);
Alternatively, you could use a static import:
import static com.your.package.MinHeapify.minHeapify;
And use the current unqualified method call notation.
Well minHeapify is a method is the class MinHeapify. You can't access it in the class ReservationArray unless you use the reference to MinHeapify to access it.

Trying to access an array from ArrayList

I'm trying to Sort an array from my ArrayList:
ArrayList<Integer> al = new ArrayList<Integer>();
al.insert(0, 4);
al.insert(1, 3);
al.insert(2, 2);
al.insert(3, 1);
SelectionSortWrappers<Integer> ss = new SelectionSortWrappers<Integer>();
ss.sort(al.elements);
ss.show(al.elements);
But when I try to access al.elements, I'm getting:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
Here is my SelectionSort Class:
public class SelectionSortWrappers<T>{
public <T extends Comparable<? super T>> void sort(T[] array){
int index;
for(int i = 0 ; i < array.length;i++){
index = i;
for(int j = i + 1; j < array.length; j++){
if (array[j].compareTo(array[index]) < 0){
index = j;
}
}
T smaller = array[index];
array[index] = array[i];
array[i] = smaller;
}
}
public void show(T[] array){
for(int i=0; i < array.length; i++){
System.out.print(array[i] + " ");
}
}
}
My ArrayList, i had to create, because is for my university project, i cannot use the Java one.
package Lists;
public class ArrayList<T> implements List<T> {
private static int MAX_SIZE = 10;
private static final int NOT_FOUND = -1;
public T[] elements;
protected int size;
public ArrayList() {
size = 0;
elements = (T[]) new Object[MAX_SIZE];
}
public T[] getArray(){
return elements;
}
public int find(T v) {
for(int i = 0; i < size; i++) {
if(v == elements[i]) {
return i;
}
}
return NOT_FOUND;
}
public T elementAt(int pos) {
if(pos >= 0 && pos < size) {
return elements[pos];
}
throw new InvalidArgumentException();
}
public void insert(int pos, T v) {
if (size == MAX_SIZE){
elements = Arrays.copyOf(elements, size * 2);
MAX_SIZE = size * 2;
}
if(pos == size) {
elements[size] = v;
}
else {
for(int i = size; i > pos; i--) {
elements[i] = elements[i-1];
}
elements[pos] = v;
}
size++;
}
public void remove(int pos) {
if(pos >= 0 && pos < size) {
for(int i = pos; i < size-1; i++) {
elements[i] = elements[i+1];
}
size--;
}
else {
throw new InvalidArgumentException();
}
}
public int size() {
return size;
}
public void show(boolean reverse) {
if (!reverse){
for(int i=0; i < size; i++){
System.out.print(elements[i] + " ");
}
} else {
for(int i=size; i >= 0; i--){
System.out.print(elements[i] + " ");
}
}
}
}
Where is the problem? My elements field is public.
You're running into the predictable erasure-versus-arrays problem caused by doing (T[]) new Object[MAX_SIZE]. You'll get a warning on that line -- that warning is warning you about exactly this problem.
Your ArrayList class is pretending an Object[] is a T[], but it really isn't -- the actual referenced array is still an Object[]. When you pull it out with al.elements, it tries to actually cast it to an Integer[] and fails.
You will have to do something ugly to deal with this -- like what the built-in java.util.Collection.toArray(T[]) has to do, for example. Alternately, you could write your sorting method to access your ArrayList directly instead of trying to work on its underlying array.

Categories

Resources