Hi everyone,
I want to write a static method which will take a partially filled char array and an int size which gives the number of positions used in array. This method will delete all repeated elements from the array and return the new size of the array.For example following code:
char a[10];
a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';
int size = 4;
size = deleteRepeats(a, size);
After executing this code, the value of a[0] is 'a', the value of a[1] is 'b', the value of a[2] is 'c', and the value of size is 3.
I wrote following code for this problem and it does not work properly, it gives wrong results after calling the code. What is the problem with following code:
Code:public class L3S1 { public static void main(String[] args) { char [] a=new char[10]; a[0]='a'; a[1]='b'; a[2]='d'; a[3]='c'; a[4]='c'; a[5]='d'; int newSize=deleteRepeats(a,6); System.out.println(newSize); } public static int deleteRepeats(char[]array,int size){ int result=size; for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ if(array[i]==array[j]){ for(int k=j+1;k<size;k++){ array[j]=array[k]; result--; } } } } return result; } }
After calling the function, your array is "abdcdd".. so it's definitely not right.
that's how i would do it:
prints out:Code:import java.util.ArrayList; public class L3S1 { public static void main(String[] args) { ArrayList<Character> array = new ArrayList<Character>(); array.add('a'); array.add('c'); array.add('b'); array.add('d'); array.add('b'); array.add('d'); array.add('c'); array.add('c'); array.add('b'); array.add('d'); array.add('c'); array.add('c'); array.add('a'); int newSize=deleteRepeats(array); System.out.println(newSize); System.out.println(array); } public static int deleteRepeats(ArrayList<Character> array){ for(int i = 0; i < array.size(); i++){ if(array.lastIndexOf(array.get(i)) != i){ array.remove(array.lastIndexOf(array.get(i))); i--; } } return array.size(); } }
Code:4 [a, c, b, d]
Ok, thanks but it is of course much more more more ... and more easier to do it with ArrayList. You just make use of the methods of ArrayList and find a way to solve the problem.
But the main problem here is to do it with Arrays and I actually do not care about the elements that come after the (size)th element.
printsCode:public class L3Sl { public static void main(String[] args) { char [] a=new char[10]; a[0]='a'; a[1]='b'; a[2]='d'; a[3]='c'; a[4]='c'; a[5]='d'; int newSize=deleteRepeats(a); System.out.println(newSize); System.out.print(a); } public static int deleteRepeats(char[]array){ int size = 0; for(int i=0; i<array.length; i++){ for(int j=i+1; j<array.length; j++){ if(array[j] == array[i]) array[j] = 0; } } for(int i=0; i<=array.length; i++){ if(array[i] != 0 ) size++; } return size; } }
for the future... it is easy to test which characters are unused in char array, because their integer or ASCII value is 0.Code:4 abdc![]()
Just a thought: in your deleteRepeats function, you are using size as a loop limit, when the size of your array is actually changing. You change result to reflect those changes, but not size. That just feels like a bug waiting to happen.
I think i finally understood what you want.
Code:public class Test { public static void main(String[] args) { char [] a=new char[10]; a[0]='a'; a[1]='b'; a[2]='c'; a[3]='c'; a[4]='c'; a[5]='d'; int newSize=deleteRepeats(a, 3); System.out.println(newSize); } public static int deleteRepeats(char[]array, int size){ int count=0; char arrayTemp[] = new char[array.length]; // lets make a copy of the array, so we won't change it. for(int i=0; i<size; i++){ arrayTemp[i] = array[i]; } //clear all slots with repeating chars for(int i=0; i<size; i++){ for(int j=i+1; j<size; j++){ if(arrayTemp[j] == arrayTemp[i]) arrayTemp[j] = 0; } } //lets "cut" the caps inside of the array char [] a=new char[arrayTemp.length]; int charcount=0; for(int i=0; i<size; i++){ if(arrayTemp[i] != 0){ a[charcount] = arrayTemp[i]; charcount++; } } for(int i=0; i<arrayTemp.length; i++){ arrayTemp[i] = a[i]; } // count the chars left for(int i=0; i<size; i++){ if(arrayTemp[i] != 0 ) count++; } return count; } }
Last edited by Sinipull; 07-05-2009 at 06:43 AM.
Thank you very much, thats definitely what I want to do![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks