easy remove method

Jacky124

Neues Mitglied
Hello,


I have to do some tasks for school according linked lists and trees in Java.


I have a Main Class with a Tree that looks like this (first is the name, second a "phonenumber"):


Java:
public class Main {
	public static void main(String[] args) {
		BinTree bt = new BinTree();
		bt.insert("Maier", "123");
		bt.insert("Huber", "234");
		bt.insert("Zwinger", "345");
		bt.insert("Düringer", "456");
		bt.insert("Mustermann", "567");
		bt.insert("Bert", "678");
		bt.insert("Sochi", "789");
		bt.insert("Vetter", "890");
		bt.inorder();	
	}
}


And in another class called BinTree I have a method for inserting entrys and one for putting the list in order.
Here's the code:


Java:
public class BinTree {	
	private ContactNode root;
	
	public void insert (String name, String phone) {
		ContactNode n = new ContactNode();
		n.setName(name);
		n.setPhone(phone);
		
		ContactNode prev = null;
		ContactNode p = root;
		
		while(p != null) {
			prev = p; 
			if(n.getName().compareTo(p.getName()) < 0) {
				p = p.getLeft();
			} else {
				p = p.getRight();
			}
		}
		
		if(prev == null) {
			root = n;
		} else {
			if(n.getName().compareTo(prev.getName()) < 0) {
				prev.setLeft(n);
			} else {
				prev.setRight(n);
			}
		}
	}
	
	public void inorder () {
		inorder(root);
	}
	
	private void inorder(ContactNode p) {
		if(p!=null) {
			inorder(p.getLeft());
			System.out.println(p.getName() + " (" + p.getPhone() + ")");
			inorder(p.getRight());
		}
	}
}


okay, that works! The task I can't finish is the remove method. I have to look for a name in the list (public void remove(String name) {}) and then this name should be removed.


And I also already know the way I want to do it: I want to get the previous ContactNode, where I already have a variable (somewhere) and then put the one next in row from my current ContactNode (which would be the one at the right, so something with getRight()... if I'm understanding it correctly) as the next node.


But I just can't get the freaking syntax right! &"!($("@ :grr:


I know I have to make an if-statement looking somehow like the one above:


Code:
if(n.getName().compareTo(name()) < 0) {
		//do something
	}


I am comparing the name of the current node to the one I got through the remove() method, and if they match something should happen. The currentNode should get removed!


Please, if anybody can help me out here I would be really grateful!




All the best
Jacky
 
Zuletzt bearbeitet von einem Moderator:

Ch4t4r

Aktives Mitglied
Hello Jacky,

you *might* have noticed that this is a german website, but most of the people are going the help you anyways.

To compare strings your are using the equals or equalsIgnoreCase (which of course ignores the case when comparing) method. The compareTo method is used for sorting thus returns which item of the compared ones is 'greater' than the other.

Greetings,
Daniel
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
B checkboxes EASY Java Basics - Anfänger-Themen 2
krgewb remove in for Java Basics - Anfänger-Themen 5
D remove Object von einer Liste von Obejcts Java Basics - Anfänger-Themen 3
G Java LinkedList remove Methode Java Basics - Anfänger-Themen 5
D remove arraylist by id not work Java Basics - Anfänger-Themen 6
H Collections JTree remove zeigt keinen Effekt! Java Basics - Anfänger-Themen 8
B Sorting List und Remove Java Basics - Anfänger-Themen 2
P Map - remove() ? Java Basics - Anfänger-Themen 46
Hacer remove at index Java Basics - Anfänger-Themen 31
J Methoden Doppelt verkettete Liste remove(Object) Java Basics - Anfänger-Themen 8
S Vererbung remove elment from Array Java Basics - Anfänger-Themen 0
K Methoden Probleme mit LinkedList.remove(object) Java Basics - Anfänger-Themen 1
U PriorityQueue remove Java Basics - Anfänger-Themen 2
M ArrayList - remove() löscht nicht! - weiß nicht wieso! Java Basics - Anfänger-Themen 8
B map.remove(long) Java Basics - Anfänger-Themen 5
Q queue.remove Element trotzdem noch vorhanden. Java Basics - Anfänger-Themen 10
B LinkedList remove Java Basics - Anfänger-Themen 5
I Liste Remove erstes Element Java Basics - Anfänger-Themen 5
D Wie Iterator Remove implementieren? Java Basics - Anfänger-Themen 11
W Treemap remove Java Basics - Anfänger-Themen 5
H Remove Methode von List Java Basics - Anfänger-Themen 6
W ArrayLists: Verständnisproblem bei remove() Java Basics - Anfänger-Themen 2
C Datentypen ArrayList.remove(index) hinterlässt leeres Feld Java Basics - Anfänger-Themen 5
H TreeMap.remove(value) - und nicht mit key! Geht das? Java Basics - Anfänger-Themen 18
S OOP ArrayList Klasse nachbauen, prob mit remove Java Basics - Anfänger-Themen 5
U ArrayList.remove(i) funktioniert nicht Java Basics - Anfänger-Themen 9
J Remove from an AraayList Java Basics - Anfänger-Themen 8
J Iterator remove()? Java Basics - Anfänger-Themen 5
T ArrayList#remove Java Basics - Anfänger-Themen 8
M ArrayList remove Element? Java Basics - Anfänger-Themen 3
B HashMap remove Java Basics - Anfänger-Themen 2
J TreeSet methode Remove Java Basics - Anfänger-Themen 13
C Add / Remove Panel Java Basics - Anfänger-Themen 2
J remove Problem Java Basics - Anfänger-Themen 8
M remove()-Methode eines Iterators Java Basics - Anfänger-Themen 3
megachucky remove bei Vector klappt nicht Java Basics - Anfänger-Themen 11
M Problem mit Iterator.remove() Java Basics - Anfänger-Themen 5
R finaler Wert in outerMethod von method in InnerMethodClass Java Basics - Anfänger-Themen 2
S Methoden 2 non-static Methoden, trotzdem Fehler "non static method can not be referenced from a static context" Java Basics - Anfänger-Themen 9
P myClass?.method() in Java Java Basics - Anfänger-Themen 4
T setFill method Java Basics - Anfänger-Themen 3
H Methode mit Array als Rückgabe This method must return a result of Type int[] Java Basics - Anfänger-Themen 2
N The method setSaldo(double) in the type Konto is not applicable for the arguments (int, int) Java Basics - Anfänger-Themen 2
A startsWith method und substring Java Basics - Anfänger-Themen 2
W Remote Method Invocation RMI - Problem Java Basics - Anfänger-Themen 0
I Java Generics factory method Java Basics - Anfänger-Themen 2
V the static method should be accessed is a static way Java Basics - Anfänger-Themen 6
N Erste Schritte "non-static method" oder "XYZ can not be resolved" Java Basics - Anfänger-Themen 21
K Eigene Annotations, Pre-/Post-/Call-Method Java Basics - Anfänger-Themen 6
B Threads Thread sleep() Method einfache Frage Java Basics - Anfänger-Themen 8
G Was als main Method Java Basics - Anfänger-Themen 6
M Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 10
E Array to String Method Reverse Java Basics - Anfänger-Themen 5
D Methoden undefined method Java Basics - Anfänger-Themen 13
S Method mit Eingabe abrufen Java Basics - Anfänger-Themen 1
D Interface Amazon Skill Kit, Interface but method in other class? Java Basics - Anfänger-Themen 3
U Method References Java Basics - Anfänger-Themen 1
UnityFriday method getPrevious in class List<ContentType> cannot be applied to given types Java Basics - Anfänger-Themen 29
P Variablen einer Methode in andere Method übergeben Java Basics - Anfänger-Themen 6
B Methoden The method mirror(double[]) in the type Convolution is not applicable for the arguments (double) Java Basics - Anfänger-Themen 8
M abstract method does not override or implement.... Java Basics - Anfänger-Themen 7
R The method printf(String, Object[]) in the type printStrem in not applicable for the arguments ... Java Basics - Anfänger-Themen 3
M Deklaration und Initialisierung bei Method Erstellung Java Basics - Anfänger-Themen 12
K Variablen RETURN in Case-Switch / This method must return a result of type Item Java Basics - Anfänger-Themen 4
W Compiler-Fehler "non-static method cannot be referenced"-Problem Java Basics - Anfänger-Themen 6
K String equalIgnoreCase() Method Java Basics - Anfänger-Themen 19
E non-static method und static context Java Basics - Anfänger-Themen 15
L Command Prompt / Main Method / String Java Basics - Anfänger-Themen 9
M Methoden "Non-static method xy cannot be referenced from a static context" Java Basics - Anfänger-Themen 20
T selection method does not contain a main type Java Basics - Anfänger-Themen 7
U UML Factory Method Java Basics - Anfänger-Themen 4
R Compiler-Fehler Cannot find symbol (Method printIn) Java Basics - Anfänger-Themen 3
H non-static method cannot be referenced from a static context Java Basics - Anfänger-Themen 2
L Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 6
B Object "Method" in TreeSet, Fehler beim Vergleichen/Comparable Java Basics - Anfänger-Themen 9
M Problem mit Static Method Java Basics - Anfänger-Themen 8
W Main-method in Object-classes Java Basics - Anfänger-Themen 5
J Compiler findet method nicht Java Basics - Anfänger-Themen 12
M This method must return a result of type int Java Basics - Anfänger-Themen 13
M Fehlermeldung: the method.... ist undefined for the type object Java Basics - Anfänger-Themen 6
S this method must return a result of type double Java Basics - Anfänger-Themen 2
G Collections.binarySearch(LinkedList): cannot find method Java Basics - Anfänger-Themen 6
Y Theorie: Abstract Method Java Basics - Anfänger-Themen 6
C Methoden numberField.getDouble no such method error Java Basics - Anfänger-Themen 4
F Mal wieder: Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 9
F Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 3
Q non-static method blub cannot be referenced from a static context Java Basics - Anfänger-Themen 6
E Netbeans, "class does not have a main method" Java Basics - Anfänger-Themen 8
L no such method error: main() Java Basics - Anfänger-Themen 2
O Abstract Method & Generics Java Basics - Anfänger-Themen 10
N public class -> public method -> variable private? Java Basics - Anfänger-Themen 10
R Could not find main method Java Basics - Anfänger-Themen 3
U Boolean istGleich() Method Java Basics - Anfänger-Themen 15
Luk10 method () not found Java Basics - Anfänger-Themen 9
A OOP Fehler non-static method can not be[...] Java Basics - Anfänger-Themen 2
N Reference to non-static method Java Basics - Anfänger-Themen 8
G non static method scale(int,int) cannot be referenced from a static context Java Basics - Anfänger-Themen 16
A The method getYear() from the type Date is deprecated Java Basics - Anfänger-Themen 6
A cannot find symbol - symbol : method Java Basics - Anfänger-Themen 5
S The method readInt() Problem Java Basics - Anfänger-Themen 5

Ähnliche Java Themen

Neue Themen


Oben