我試圖做一個類,我把一個關鍵和值放入put方法,把k字符串數組中的鍵和值放入v字符串數組中,但它當我得到或顯示時沒有被保存在數組中。
例如:put(dan,30)get(dan)返回null
顯示返回null null 10次。有誰知道什麼是錯的?價值沒有被保存在字符串
public class Memory
{
final int INITIAL_CAPACITY = 10;
String[] k = new String[INITIAL_CAPACITY];
String[] v = new String[INITIAL_CAPACITY];
int count = 0;
public Memory()
{
count = 0;
}
public int size()
{
return count;
}
public void put(String key, String value)
{
int a = 0;
boolean found = false;
for (int i = 0; i < k.length; i++)
{
//System.out.println("key is " + key.equals(k[i]));
if (key.equalsIgnoreCase(k[i]))
{
v[i] = value;
found = true;
}
if (found)
break;
a++;
}
//System.out.println(a == k.length);
if (a == k.length);
{
k[count] = key;
v[count] = value;
//System.out.println(k[count] + " " + v[count]);
count++;
//System.out.println(count);
}
}
public String get(String key)
{
String output = "a";
for(int i = 0; i < k.length; i++)
{
if(!key.equalsIgnoreCase(k[i]))
{
output = null;
}
else
{
output = v[i];
return output;
}
}
return output;
}
public void clear()
{
for (int i = 0; i < k.length; i++)
{
k[i] = null;
v[i] = null;
}
count = 0;
}
public void display()
{
for (int i = 0; i < k.length; i++)
{
System.out.println(k[i] + " " + v[i]);
}
}
}
您是否試過單步執行代碼以查看它出錯的位置? – David 2010-04-09 23:05:34
put方法不起作用,因爲陣列中沒有保存任何東西,並且count也沒有遞增 – Raptrex 2010-04-09 23:11:59
您用來調用mehtods的代碼是什麼? – willvv 2010-04-09 23:13:50