| | |
ReplaceAll Method
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 1
Okay so this is the deal. I want to make a call to a routine that replaces all of one certain type of integer with a number. I have it to where it is replaceing the first, however, it misses the tail node.
to make the call to the method I use
the list contains 1,1,2,2,3,3. Its printing out 1,1,88,2,3,3.
is my logic wrong? any helpful hints?
btw .head() and .tail() are part of a class called SimpleList. It just points to the first, and next node.
public static SimpleList<Integer> replaceAll(Integer old, Integer nEw, SimpleList<Integer> l)
// returns a copy of l with all occurrences of old replaced with nEw
{
if(l.isEmpty())
return new SimpleList<Integer>();
else if (l.head().equals(old))
return new SimpleList<Integer>(nEw, l.tail());
if (l.tail().equals(old))
return new SimpleList<Integer>(nEw, l.tail());
else
return new SimpleList<Integer>(l.head(), replaceAll(old, nEw,l.tail()));to make the call to the method I use
System.out.println(Proj7.replaceAll(new Integer(2),new Integer(88),l1)); the list contains 1,1,2,2,3,3. Its printing out 1,1,88,2,3,3.
is my logic wrong? any helpful hints?
btw .head() and .tail() are part of a class called SimpleList. It just points to the first, and next node.
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 1
I don't have a solution for you because I don't know enough about how this SimpleList class is working to understand why you would be returning a new reference that you create by using the nEw Integer and the tail of the list. Is there some reason you need to use a recursive call like is this for a project or something, if not you would be way better off just iterating through the list and doing comparisons? I think if you want help you're probably going to have to put the implementation of the constructors you're calling up on the forum.
ah sorry didn't see you're old post initially I'll look through that and see if I have a solution
ah sorry didn't see you're old post initially I'll look through that and see if I have a solution
Last edited by shaun.husain; Dec 4th, 2008 at 9:50 pm.
![]() |
Similar Threads
- Java's String Tokenizer (Java)
- String.replace() Method (Java)
- Help with replace method. (Java)
- How to replace repeated substring in other string? (Java)
- Problem with String replaceAll method (Java)
Other Threads in the Java Forum
- Previous Thread: Recursion Problem
- Next Thread: Combinations
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component database developmenthelp eclipse error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online pearl problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor





