
12-01-2008, 01:21 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
help to understand this problem clear
a problem i get into a book entitled Absolute JAVA.
|
Quote:
|
Write a static method called deleteRepeats that hasa partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Because a partially filled array requires two arguments, the method should actually have two parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted, the remaining letters are moved up one position to fill in the gap. This creates empty positions at the end of the array so that the less of the array is used. Because the formal parameter is partially filled array, a second formal parameter cannot be changed by a JAVA method, so have the method return the new value for this parameter. For example, consider the following code:
|
Code:
|
char a[10];
a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';
int size=4;
size = deleteRepeats(a, size); |
After this code is executed, the value of a[0] is 'a', the value os [1] is 'b'. the value of a[2] is 'c', and the value of size is 3.(The value of a[3] is no longer of any concern, because the partially filled array no longer uses this indexed variable.)
|
here is the problem..
i cant understand it more clearly..
is there anyone who could summarize this in order for me to understand it clearly?
i dont said that you write a program for me..
i just said is to help me understand this problem clearly..
help
|
|

12-01-2008, 01:28 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
|
|
|
Something like this,
You have an array of char types. You can added up to 10 elements there. Another variable is also there to handle the number of filed index in the array, type of int. You cannot have duplicate letters.
|
|

12-01-2008, 01:35 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
is this work:
|
Code:
|
char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
int x; |
x as the variable that handle the number of filled index in the array..
is that what you trying to say?
|
|

12-01-2008, 01:41 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
|
|
|
Yes, simply it's hold the number of indexes without duplicating.
|
|

12-01-2008, 01:50 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
i came up w/ this code:
|
Code:
|
public class Exercise
{
public static void main(String[]args)
{
char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
int x;
for(x=i;x<letter.length;x++)
System.out.println(x);
}
} |
i want to print those values in my array using the int x.
but it doesnt work..
what was the problem?
Last edited by beginner21; 12-01-2008 at 01:50 PM.
Reason: tag
|
|

12-01-2008, 02:07 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
|
|
|
What is that value i?Where did you get the variable i?
Last edited by serjant; 12-01-2008 at 02:12 PM.
|
|

12-01-2008, 02:11 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
|
that is my problem..
how can i print the values in the array using the int x?
as you can see in my previous post, the code doesn't work..
i want to print the values inside the array but the i has no value..
what should i do?
|
|

12-01-2008, 02:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,446
Rep Power: 11
|
|
|
What the error message you get? Where you define the variable i?
|
|

12-01-2008, 02:27 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
here is the error message i got:
|
Code:
|
Num4.java:7: cannot resolve symbol
symbol : variable i
location: class Num4
for(x=i;x<letter.length;x++)
^ |
i think the error said that where i initialized variable i?
is there another way how to print the values in the array?
Last edited by beginner21; 12-01-2008 at 02:27 PM.
Reason: spacing
|
|

12-01-2008, 02:40 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
|
|
it should be
|
Code:
|
for(x=0;x<letter.length;x++) |
|
|

12-01-2008, 02:49 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
|
|
hhhmm...
- First of all, it is good practice to put curly brackets "{}" after for & if statements, even if they're one liners.
- Next, get rid of the i variable.
- Initialize the x variable in you for statement as you would in any other for statement.
- If you want to print the array elements, you can use the x variable:
|
Code:
|
System.out.println("letter[" + x + "] = " + letter[x]); |
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-01-2008, 02:51 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
|
Quote:
|
it should be
|
Code:
|
for(x=0;x<letter.length;x++) |
|
it wont work,
the output in that code display 0-9..
0123456789
because you declare the x as 0 not the index of the array...
Last edited by beginner21; 12-01-2008 at 02:52 PM.
Reason: tag
|
|

12-01-2008, 02:57 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
tnx CSJLMAN..
your code works
my modified code is this:
|
Code:
|
public class Exercise
{
public static void main(String[]args)
{
char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
int x;
for(x=0;x<letter.length;x++)
System.out.println("letter[" + x + "] = " + letter[x]);
}
} |
what was the next based on the problem?
i cant understand what should be next after filling the index of array.
is it im going to do now the method deleteRepeats();?
|
|

12-01-2008, 03:20 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
|
|
Let's see if you understood...
Before you can go to the next step (methods), you have to understand what you just did.
Questions: - Do you know why my statement in my last post worked for you?
- Serjant's code was correct, he was correcting an error you had in your for statement.
- Do you completely understand everything that in the the println statement?
Statement: you didn't follow my suggestion about using the curly brackets "{}" for the "for" statement.
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-01-2008, 03:23 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
sorry CJSLMAN
ok here is my modify code:
|
Code:
|
public class Exercise
{
public static void main(String[]args)
{
char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
int x;
for(x=0;x<letter.length;x++)
{
System.out.println("letter[" + x + "] = " + letter[x]);
}
}
} |
sorry again,
is that what you're trying to say?
it works also, is the method is the next?
|
|

12-01-2008, 03:49 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
|
|
Did you understand?
I just want to be sure you understand how yoiur code is working. - Did you understand what changed in you code to make it working that way you wanted it to? Do NOT be satisfied that it is working without understanding why.
- Yes, creating a method is next. Here's some help:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-01-2008, 04:04 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
|
of course i understand it..
x is now the index of my array,
print new line after every execution of the loop because the loop will continuously executing until the condition satisfies,
is it right?
for the method,i cant understand what should i write inside deleteRepeats();?
and where did this method return?
|
|

12-01-2008, 05:44 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,160
Rep Power: 3
|
|
Yes... that is the function of the println... I'm more interested if you understand what's in the println.
Anyway....
Your original question was to understand the book assignment:
- Original array has the following elements {'a','b','a','c'}.
- delete the repeated letters/elements in the array
- move the remaining elements to fill the empty space left by the deleted element
- return the new size of the array (the number of remaning elements
- Example:
- Original array = {'a','b','a','c'}
- Array after deleting repeated elements = {'a','b', ,'c'} or { ,'b','a','c'}
- Array after moving elements = {'a','b','c', ,}
- return size = 3
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 12:18 AM.
|
|