Results 1 to 18 of 18
- 12-01-2008, 12:21 PM #1
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.
here is the problem..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:
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.)Java Code:char a[10]; a[0] = 'a'; a[1] = 'b'; a[2] = 'a'; a[3] = 'c'; int size=4; size = deleteRepeats(a, size);
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, 12:28 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 12:35 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
is this work:
x as the variable that handle the number of filled index in the array..Java Code:char[] letter = {'a','b','c','d','e','f','g','h','i','j'}; int x;
is that what you trying to say?
- 12-01-2008, 12:41 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, simply it's hold the number of indexes without duplicating.
- 12-01-2008, 12:50 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
i came up w/ this code:
i want to print those values in my array using the int x.Java 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); } }
but it doesnt work..
what was the problem?Last edited by beginner21; 12-01-2008 at 12:50 PM. Reason: tag
- 12-01-2008, 01:07 PM #6
What is that value i?Where did you get the variable i?
Last edited by serjant; 12-01-2008 at 01:12 PM.
- 12-01-2008, 01:11 PM #7
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, 01:21 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What the error message you get? Where you define the variable i?
- 12-01-2008, 01:27 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
here is the error message i got:
i think the error said that where i initialized variable i?Java Code:Num4.java:7: cannot resolve symbol symbol : variable i location: class Num4 for(x=i;x<letter.length;x++) ^
is there another way how to print the values in the array?Last edited by beginner21; 12-01-2008 at 01:27 PM. Reason: spacing
- 12-01-2008, 01:40 PM #10
it should be
Java Code:for(x=0;x<letter.length;x++)
- 12-01-2008, 01:49 PM #11
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:
Luck,Java Code:System.out.println("letter[" + x + "] = " + letter[x]);
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 01:51 PM #12
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
it wont work,it should be
Java Code:for(x=0;x<letter.length;x++)
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 01:52 PM. Reason: tag
- 12-01-2008, 01:57 PM #13
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
tnx CSJLMAN..
your code works
my modified code is this:
what was the next based on the problem?Java 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]); } }
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, 02:20 PM #14
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 02:23 PM #15
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
sorry CJSLMAN
ok here is my modify code:
sorry again,Java 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]); } } }
is that what you're trying to say?
it works also, is the method is the next?
- 12-01-2008, 02:49 PM #16
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 03:04 PM #17
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, 04:44 PM #18
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
clear cache
By Jadellll in forum New To JavaReplies: 0Last Post: 03-20-2008, 09:27 AM -
Please clear my doubt...............
By vinaytvijayan in forum Advanced JavaReplies: 0Last Post: 12-26-2007, 06:23 PM -
how to clear the data of an object
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 10:50 PM -
Help, someone clear up Interfaces for me
By mathias in forum New To JavaReplies: 1Last Post: 08-06-2007, 02:26 AM -
I want to do is clear the console
By paul in forum Advanced JavaReplies: 7Last Post: 08-03-2007, 06:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks