user1277281
user1277281

Reputation: 117

How to find every occurrence of a target value in array and return a new array containing the indexes for these values?

I am working on a problem and am having a hard time getting it to output the right information.

What I am trying to do is find every occurrence of a target value and put it into a new array and output the indexes of the target values. If no target value is found it returns an empty array. outputs {}

Right now I am getting an error.

findAll(): 
[I@349b688e
[I@46ed5d9d
java.lang.ArrayIndexOutOfBoundsException

The outputs should be:

outputs {0, 5}

outputs {}

public class FindIndex(){
public FindIndex() {
int a[] = {7, 8, 9, 9, 8, 7};

System.out.println("findAll(): ");
System.out.println(findAll(a, 7));
System.out.print(findAll(a, 2));
}

public int[] findAll(int a[], int num) {

    int indexNum = 0;
    int arrSize = 0;
    
 
    // find all occurrence of the target number
    for(int i = 0; i < a.length; i++) {
        if(a[i] == num) {
            arrSize++;
        }
    }
    
    // create new array 
    int newArray[] = new int[arrSize];
    for(int i = 0; i < a.length; i++) {
        if(a[i] == num) {
            newArray[indexNum] = i;
            
        }
    }
    return newArray;
}

public void print(int a[]) {
    System.out.print("{");
    int i;
    
    for(i = 0; i < a.length - 1; i++) {
        System.out.print(a[i] + ", ");
    }
    
    if(a.length > 0) {
        System.out.print(a[i]);
    }
    System.out.print("}\n");

}
}

Upvotes: 0

Views: 1373

Answers (4)

user1277281
user1277281

Reputation: 117

I was working with the code and got it to work but it is giving me added zeros in the new array. The output of the top sould be 0, 5 How do I get ride of these added zeros?

int a[] = {7, 8, 9, 9, 8, 7}; 
int array2[];

// print the occurrence of a target index value 
    System.out.println("findAll(): ");
    array2 = copy(findAll(a, 7));
    array2 = copy(findAll(a, 2));

public int[] findAll(int a[], int target) {

    int index;
    int found = 0;

   // find all occurrence of the target number
    for(int i = 0; i < a.length; i++) {
        if(a[i] == target) {
            found = target;
            i++;
        } 
    } 

    // create new array 
    int newArray[] = new int[found];
    for(int i = 0; i < newArray.length; i++) {
        if(a[i] == target) {
            newArray[i] = i;
            i++;
        }
    }
   return newArray;
} 

public int[] copy(int newArray[]) {
    System.out.print("{");
    int i;

    for(i = 0; i < newArray.length - 1; i++) {
        if (newArray.length == 0) {
            System.out.print( " " );
        } else {
            System.out.print(newArray[i] + ", ");
        }

    }


    System.out.print("}\n");
    return newArray;
 }


output:

findAll(): 
{0, 0, 0, 0, 0, 5, }
{}

Upvotes: 0

Roman C
Roman C

Reputation: 1

Calling findAll(a, 2) gets zero size newArray that's why you get ArrayIndexOutOfBoundsException.

You should check if (arrSize > 0) before doing array with indexes and access it.

Upvotes: 0

user2030471
user2030471

Reputation:

You are never incrementing indexNum, it always remains at 0 and the array keeps on writing the values at this same index.

I think you should have this expression there:

newArray[indexNum++] = i;

Upvotes: 0

Sean Landsman
Sean Landsman

Reputation: 7177

For your printing problem, what you're actually printing out ther are the addresses of the arrays returned, not the contents.

What you can do is the following:

    System.out.println(Arrays.toString(findAll(a, 7)));
    System.out.print(Arrays.toString(findAll(a, 2)));

This will give you the following output:

[5, 0]
[]

Upvotes: 0

Related Questions