# Thread: Problem with vector and if statement comparision

1. Member
Join Date
Mar 2012
Posts
1
Rep Power
0

## Problem with vector and if statement comparision

this is a school assignment however im taking an independent study so it is one i have tasked myself to learning before a programming competition in the next couple of months, for the problem ive decided to use recursive method to sort the numbers however i want to use a vector to remove duplicates, and it isnt doing so here is the code, the first number inputted determines the size of the array then you enter that many numbers until it stops EXAMPLE OUTPUT:{input:5 -100 3 2 4 4 }-100 2 3 4 4 intead of -100 2 3 4
Java Code:
```import java.util.*;
public class Problem3 {

static int[]finalArray;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int listLength = sc.nextInt();
int[] input = new int[listLength];
for (int i=0;i<input.length;i++){
input[i] = sc.nextInt();
}
sort(input);
removeDuplicates(input);
for (int i=0;i<finalArray.length;i++){
System.out.print(finalArray[i]+" ");
}
}
private static void sort(int[]data){
for(int i = 0; i < data.length; i++) {
int min = i;
for(int j = i + 1; j < data.length; j++) {
if(data[j] < data[min])
min = j;
}
int t = data[min];
data[min] = data[i];
data[i] = t;
}
}
private static void removeDuplicates(int[]data){
Vector<Integer> vector = new Vector<Integer>();
for (int temp=0;temp<data.length;temp++){
vector.addElement(new Integer(data[temp]));//convert array into vector to remove data
}
for(int i = 0;i<vector.size();i++){
for (int j=0;j<vector.size();j++){

//System.out.println("Element at(i): "+vector.elementAt(i)+"\nElement at J: "+vector.elementAt(j));// debugging that i added, this is not nessessary
if(vector.elementAt(i)== vector.elementAt(j)&& j!=i){//i believe the problem is here
//System.out.println("it should delete here");
vector.remove(j);
}
}
}
finalArray = new int[vector.size()];
for (int l=0;l<finalArray.length;l++){
finalArray[l]=vector.elementAt(l);
}
}
}```
Last edited by zachnifane; 03-02-2012 at 05:35 PM. Reason: no example output

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,642
Rep Power
6

## Re: Problem with vector and if statement comparision

Unless you have reason otherwise, when comparing objects you should use the equals method as opposed to == (the latter compares whether the references are the same, not the object values)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•