# Character Array Question

Page 1 of 1

## 9 Replies - 5043 Views - Last Post: 01 December 2010 - 09:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=203012&amp;s=26f1adb6bb90614c5053c490df4350ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 deepakl_2000

Reputation: -1
• 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

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #3 deepakl_2000

Reputation: -1
• 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

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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);

```

### #5 deepakl_2000

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

## Re: Character Array Question

Posted 01 December 2010 - 08:59 AM

macosxnerd101, 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.

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #7 kngofdrkns

Reputation: 7
• 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

### #8 g00se

• D.I.C Lover

Reputation: 3515
• Posts: 15,904
• 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

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #10 g00se

• D.I.C Lover

Reputation: 3515
• Posts: 15,904
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }