Dies ist eine statische Kopie unseres alten Forums. Es sind keine Interaktionen möglich.
This is a static copy of our old forum. Interactions are not possible.

double

Trainee

  • "double" is male
  • "double" started this thread

Posts: 43

Date of registration: May 12th 2004

Location: Barsinghausen

1

Thursday, July 1st 2004, 4:56pm

Java - Allgemeine Fragen II

Warum entfernt die Methode
public Object remove(Object key)

für Hashtables (java.util.Hashtable) nur dann ein Element aus einer
Enumeration (hier Properties abgeleitet von Hashtable), wenn
man remove(key) gefolgt von remove(value) aufruft?

Angenommen die Prop. wären {key=value} und
man führt nun remove(key) aus und ruft
z.B. prop.list(System.out) bzw. eine entsprechende toString()
Methode auf, bekommt man soetwas wie:

{key=null}

"value" wurde also entfernt, der Schlüssel bleibt jedoch erhalten.

In der API steht jedoch:
"public Object remove(Object key)
Removes the key (and its corresponding value) from this hashtable."

--> Warum wird "corresponding value" nicht gelöscht ???

migu

free rider

  • "migu" is male

Posts: 2,643

Date of registration: Dec 11th 2001

Occupation: Developer

2

Thursday, July 1st 2004, 11:41pm

RE: Java - Allgemeine Fragen II

Quoted

Original von double
Warum entfernt die Methode
public Object remove(Object key)

für Hashtables (java.util.Hashtable) nur dann ein Element aus einer
Enumeration (hier Properties abgeleitet von Hashtable), wenn
man remove(key) gefolgt von remove(value) aufruft?


Ist das wirklich so?

Quoted

Original von double
Angenommen die Prop. wären {key=value} und
man führt nun remove(key) aus und ruft
z.B. prop.list(System.out) bzw. eine entsprechende toString()
Methode auf, bekommt man soetwas wie:

{key=null}

"value" wurde also entfernt, der Schlüssel bleibt jedoch erhalten.


Verzeih, aber ich verstehe dich nicht. Was meinst du mit "Prop"?

Ich habe Hashtable einmal getestet und die Tests laufen durch. (s.u.)
Hashtable scheint also zu funktionieren. Wo ist nun dein Problem?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class TestHashtable extends TestCase {
	Hashtable testTable;

	public static void main(String[] args) {
		junit.swingui.TestRunner.run(TestHashtable.class);
	}

	protected void setUp() throws Exception {
		super.setUp();
		testTable = new Hashtable();
		testTable.put("key0", "value0");
		testTable.put("key1", "value1");
		testTable.put("key2", "value2");
	}

	public void testNotEmpty() {
		assertFalse("Hashtable darf nicht leer sein", testTable.isEmpty());
		assertTrue(testTable.containsKey("key0"));
		assertTrue(testTable.containsKey("key2"));
		assertTrue(testTable.containsKey("key1"));
		assertTrue(testTable.containsValue("value2"));
		assertTrue(testTable.containsValue("value1"));
		assertTrue(testTable.containsValue("value0"));
		assertFalse(testTable.containsValue("nichtdrin"));
	}
	
	public void testRemove() {
		testTable.remove("key0");
		assertEquals(2, testTable.size());
		assertFalse(testTable.containsValue("value0"));

		testTable.remove("key2");
		assertEquals(1, testTable.size());
		assertFalse(testTable.containsValue("value2"));
	}
}
tar: Anlegen eines leeren Archivs wird feige verweigert.

np

Junior Schreiberling

Posts: 155

Date of registration: Oct 23rd 2002

3

Friday, July 2nd 2004, 8:47am

Hmm, migu hat recht: Das Problem konnte ich auch nicht nachvollziehen. Ich habe das Beispiel aus der Vorlesung in Zeile 12 modifiziert:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
import java.io.*;

public class PropTest {
   public static void main(String[] args) {
      try {
         FileInputStream fi = new FileInputStream("prop.conf");
         Properties prop = new Properties();
         prop.load(fi);
         fi.close();
         
         prop.remove("laf");

         String key, value;
         Enumeration namen = prop.propertyNames();
         for( ; namen.hasMoreElements(); ) {
            key = (String)namen.nextElement();
            value = prop.getProperty(key);
            System.out.println(key+" = "+value);
         }
      }
      catch(IOException ioe) {
         System.out.println("Datei "prop.conf" nicht lesbar.");
      }
   }
}
Und auf folgende Datei angewendet:

Source code

1
2
3
4
5
6
7
# initiale Größe
width = 600
height = 300
# Look-and-Feel
laf = javax.swing.plaf.metal.MetalLookAndFeel
# Menü ein oder aus?
menu = on

Ausgabe ohne Zeile 12:

Source code

1
2
3
4
height = 300
laf = javax.swing.plaf.metal.MetalLookAndFeel
width = 600
menu = on
und mit Zeile 12 (d.h. Entfernen von "laf"):

Source code

1
2
3
height = 300
width = 600
menu = on
Also wie erwartet. Getestet habe ich das unter JDK 1.5.0-beta.