Results 1 to 2 of 2
  1. #1
    zachnifane is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default 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 04:35 PM. Reason: no example output

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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)

Similar Threads

  1. Resultset comparision method
    By EADS in forum Advanced Java
    Replies: 0
    Last Post: 07-27-2011, 11:51 AM
  2. String Comparision
    By vimalaranjan in forum New To Java
    Replies: 6
    Last Post: 05-19-2011, 02:27 PM
  3. Secret of String references comparision.
    By Anjaneyulu in forum Advanced Java
    Replies: 15
    Last Post: 02-24-2010, 02:32 PM
  4. String comparision method problem
    By rons_sacramental in forum New To Java
    Replies: 7
    Last Post: 10-15-2009, 05:15 AM
  5. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM

Posting Permissions

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