Results 1 to 2 of 2
- 03-02-2012, 04:30 PM #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 04:35 PM. Reason: no example output
- 03-02-2012, 06:13 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Similar Threads
-
Resultset comparision method
By EADS in forum Advanced JavaReplies: 0Last Post: 07-27-2011, 11:51 AM -
String Comparision
By vimalaranjan in forum New To JavaReplies: 6Last Post: 05-19-2011, 02:27 PM -
Secret of String references comparision.
By Anjaneyulu in forum Advanced JavaReplies: 15Last Post: 02-24-2010, 02:32 PM -
String comparision method problem
By rons_sacramental in forum New To JavaReplies: 7Last Post: 10-15-2009, 05:15 AM -
comparision between two lists
By suprabha in forum Advanced JavaReplies: 14Last Post: 08-01-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks