Character Array Question

Character Array Question

Page 1 of 1

9 Replies - 3262 Views - Last Post: 01 December 2010 - 09:26 AM Rate Topic: -----

#1 deepakl_2000  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 01-December 10

Character Array Question

Posted 01 December 2010 - 07:54 AM

Quote

Given two character arrays a[] and b[], remove from b[] all occurrences of all characters that occur in array a[]. You need to do this in-place i.e. without using an extra array of characters. E.g.:

Input: a[] = [G, O]
Input b[] = [G, O, O, G, L, E]

Output: b[] = [L, E]

Code :

public class ReplaceCharacterArray{          
       public static void main(String args[]){         
          char a[] = [G, O]          
          char b[] = [G, O, O, G, L, E]      
 
         //to replace all the occurences of all the characters of      
         //a[] array in b[] array we have below logic.      
 
          for(int i=0;i<a.length;i++){      
             for(int j=0;j<b.length;j++){      
                  if(b[j] == a[i]){      
                    //im stuck here how do i proceed with code from here.whats wrong ?     
             }      
          }


Is This A Good Question/Topic? 0
  • +

Replies To: Character Array Question

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Character Array Question

Posted 01 December 2010 - 08:03 AM

First, you're missing a semi-colon at the end of your array declarations.

What I would do is use an ArrayList<Character>, and add a character in arrayB to the List only if it isn't found in arrayA. Then you can use the ArrayList toArray() method to convert it back into an array. Or you can using a String, and do basically the same thing, using the toCharArray() method at the end.
Was This Post Helpful? 0
  • +
  • -

#3 deepakl_2000  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 01-December 10

Re: Character Array Question

Posted 01 December 2010 - 08:20 AM

my question is with respect to remove from b[] all occurrences of all characters that occur in array a[]. why would i need an ArrayList<Character>, and add a character in arrayB to the List only if it isn't found in arrayA
I have not understood your approach,can you explain with the code please

well i dont want to use a extra arraylist here.(edited by deepak) as the question says.

Quote

Given two character arrays a[] and b[], remove from b[] all occurrences of all characters that occur in array a[]. You need to do this in-place i.e. without using an extra array of characters. E.g.:

Input: a[] = [‘G’, ‘O’]
Input b[] = [‘G’, ’O’, ’O’, ’G’, ’L’, ’E’]

Output: b[] = [‘L’, ‘E’]

This post has been edited by deepakl_2000: 01 December 2010 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Character Array Question

Posted 01 December 2010 - 08:43 AM

Arrays are static, though, so you cannot resize them. Therefore, you will have a lot of empty spaces. You either have to resize an array manually, or use an ArrayList which already has this functionality.

Here's the basic logic of this. You can look at the ArrayList documentation for the specific methods for things like adding, removing, and checking to see if the ArrayList contains elements.
ArrayList<Character> resultSet = new ArrayList<Character>();
//add all the elements in b to resultSet

for(char c: a){
    //while the resultSet contains c
           //remove c from resultSet
}

Character[] c = new Character[resultSet.size()];
c = resultSet.toCharArray(c);


Was This Post Helpful? 0
  • +
  • -

#5 deepakl_2000  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 01-December 10

Re: Character Array Question

Posted 01 December 2010 - 08:59 AM

View Postmacosxnerd101, on 01 December 2010 - 07:43 AM, said:

Arrays are static, though, so you cannot resize them. Therefore, you will have a lot of empty spaces. You either have to resize an array manually, or use an ArrayList which already has this functionality.

Here's the basic logic of this. You can look at the ArrayList documentation for the specific methods for things like adding, removing, and checking to see if the ArrayList contains elements.
ArrayList<Character> resultSet = new ArrayList<Character>();
//add all the elements in b to resultSet

for(char c: a){
    //while the resultSet contains c
           //remove c from resultSet
}

Character[] c = new Character[resultSet.size()];
c = resultSet.toCharArray(c);



well i have understood your logic,but still i dont agree with the fact that we need to go for an extra ArrayList.because
my final output should in b[] array itself.the questions says i dont have to use an extra array of characters.Output: b[] = [L, E]
hope you understand my viewpoint.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Character Array Question

Posted 01 December 2010 - 09:01 AM

You *will* need to use an extra array or ArrayList. You *cannot* reduce the size of an array without using another array.

Edit: If you don't believe me, ask your instructor if he or she disagrees with any of these suggestions. :)
Was This Post Helpful? 0
  • +
  • -

#7 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Character Array Question

Posted 01 December 2010 - 09:07 AM

if(b[j] == a[i]){
//  set b[j] to 0 then write a for loop when it finds a 0 it replaces it with the next element in the array
}


this will do what u want but the array will have empty slots at the end i your example u will get 3 empty slots in the array (waste of memory).

This post has been edited by kngofdrkns: 01 December 2010 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Character Array Question

Posted 01 December 2010 - 09:07 AM

Try something like:

    public static void main(String[] args) {
        Character[] a = { 'G', 'O' };
        Character[] b = { 'G', 'O', 'O', 'G', 'L', 'E' };

        //to replace all the occurences of all the characters of     
        //a[] array in b[] array we have below logic.     
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if (a[i].equals(b[j])) {
                    //im stuck here how do i proceed with code from here.whats wrong ?    
                    b[j] = null;
                }
            }
        }

        Character[] c = CompactArray.compact( b );
        System.out.println(java.util.Arrays.toString(c));
    }


http://technojeeves....t-array-in-java

This post has been edited by g00se: 01 December 2010 - 09:08 AM
Reason for edit:: Smilie bug still not fixed

Was This Post Helpful? 1
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Character Array Question

Posted 01 December 2010 - 09:09 AM

g00se proves my point exactly. His CompactArray class uses another array as well. :)
Was This Post Helpful? 1
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Character Array Question

Posted 01 December 2010 - 09:26 AM

Yes, of course macosxnerd101 is quite right about needing a new array.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1