This is what I have and I'm new to Java:
import java.util.*;
public class WordPairs
{
// ArrayList<String> names = new ArrayList<String>();
// ArrayList<String> meanings = new ArrayList<String>();
//names.add("empty");
//meanings.add("empty");
String searchName;
String searchMeaning;
String names[] = new String[25];
String meanings[] = new String[25];
int Q = 1;
public void setWordAdd(String name,String meaning)
{
names[Q] = name;
meanings[Q] = meaning;
Q = Q++;
}
public void setWordDelete(String name)
{
for(int i=0;i<names.length;i++)
{
String check = names[i];
if(check == name)
{
String meanings = meanings.remove(i);
}
}
}
public void setWordSearch(String name)
{
int a = 1;
for(i=0;i<names.size();i++)
{
string c开发者_StackOverflow社区heck = names.get(i);
if(check == name)
{
searchName = names.get(i);
searchMeaning = meanings.get(i);
a = 0;
}
}
if(a == 1)
{
searchName = "word can not be found";
searchMeaning = "meaning can not be found";
}
}
public String getSearchName()
{
return searchName;
}
public String getSearchMeaning()
{
return searchMeaning;
}
}
http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html
I believe you are a beginner, and by reading your code I assume you are not familiar with built-in collections yet (despite of some shy ArrayList attempt). That's ok, no one is born with that knowledge. Here is some reading for you:
Map interface: http://download.oracle.com/javase/6/docs/api/java/util/Map.html All of implementations of this interface provide functionality you need, but in different ways. For example, HashMap is useful in most cases, but if you need keys (in your code - "names") sorted, TreeMap would be better. LinkedHashMap "remembers" the order in which keys were inserted, and so on. It's very interesting and informative reading and you definitely should know about maps, because they are among most useful Java classes.
Here is introduction to Java Collections Framework in general: http://download.oracle.com/javase/tutorial/collections/index.html
Be sure to read it, because collections are irreplacable in even simplest Java applications.
I hope that helps, and good luck!
精彩评论