注意:這是一項任務。實現使用基本java數組的集合
繼續使用Java基本數組實現我的Set實現,現在我正在努力使用3到最後一個函數,即union。
import java.io.*;
class Set {
private int numberOfElements = 0;
private String[] setElements = new String[5];
private int maxNumberOfElements = 5;
// constructor for our Set class
public Set(int numberOfE, int setE, int maxNumberOfE) {
int numberOfElements = numberOfE;
String[] setElements = new String[setE];
int maxNumberOfElements = maxNumberOfE;
}
// Helper method to shorten/remove element of array since we're using basic array instead of ArrayList or HashSet from collection interface :(
static String[] removeAt(int k, String[] arr) {
final int L = arr.length;
String[] ret = new String[L - 1];
System.arraycopy(arr, 0, ret, 0, k);
System.arraycopy(arr, k + 1, ret, k, L - k - 1);
return ret;
}
int findElement(String element) {
int retval = 0;
for (int i = 0; i < setElements.length; i++) {
if (setElements[i] != null && setElements[i].equals(element)) {
return retval = i;
}
retval = -1;
}
return retval;
}
void add(String newValue) {
int elem = findElement(newValue);
if(numberOfElements < maxNumberOfElements && elem == -1) {
setElements[numberOfElements] = newValue;
numberOfElements++;
}
}
int getLength() {
if (setElements != null) {
return setElements.length;
}
else {
return 0;
}
}
String[] emptySet() {
setElements = new String[0];
return setElements;
}
Boolean isFull() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if (setElements.length == maxNumberOfElements){
return True;
} else { return False; }
}
Boolean isEmpty() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if (setElements.length == 0) {
return True;
} else { return False; }
}
void remove(String newValue) {
for (int i = 0; i < setElements.length; i++) {
if (setElements[i].equals(newValue)) {
setElements = removeAt(i,setElements);
}
}
}
int isAMember(String element) {
int retval = -1;
for (int i = 0; i < setElements.length; i++) {
if (setElements[i] != null && setElements[i].equals(element)) {
return retval = i;
}
}
return retval;
}
void printSet() {
for (int i = 0; i < setElements.length; i++) {
System.out.println("Member elements on index: "+ i +" " + setElements[i]);
}
}
String[] getMember() {
String[] tempArray = new String[setElements.length];
for (int i = 0; i < setElements.length; i++) {
if(setElements[i] != null) {
tempArray[i] = setElements[i];
}
}
return tempArray;
}
Set union(Set x, Set y) {
String[] newtemparray = new String[x.getLength];
newtemparray = x.getMember;
return x;
}
}
// This is the SetDemo class that will make use of our Set class
class SetDemo {
public static void main(String[] args) {
//get input from keyboard
BufferedReader keyboard;
InputStreamReader reader;
String temp = "";
reader = new InputStreamReader(System.in);
keyboard = new BufferedReader(reader);
try
{
System.out.println("Enter string element to be added");
temp = keyboard.readLine();
System.out.println("You entered " + temp);
}
catch (IOException IOerr)
{
System.out.println("There was an error during input");
}
/*
**************************************************************************
* Test cases for our new created Set class.
*
**************************************************************************
*/
Set setA = new Set(1,10,10);
setA.add(temp);
setA.add("b");
setA.add("b");
setA.add("hello");
setA.add("world");
setA.add("six");
setA.add("seven");
setA.add("b");
int size = setA.getLength();
System.out.println("Set size is: " + size);
Boolean isempty = setA.isEmpty();
System.out.println("Set is empty? " + isempty);
int ismember = setA.isAMember("sixb");
System.out.println("Element six is member of setA? " + ismember);
Boolean output = setA.isFull();
System.out.println("Set is full? " + output);
setA.printSet();
int index = setA.findElement("world");
System.out.println("Element b located on index: " + index);
setA.remove("b");
setA.emptySet();
int resize = setA.getLength();
System.out.println("Set size is: " + resize);
setA.printSet();
Set setB = new Set(0,10,10);
Set SetA = setA.union(setB,setA);
}
}
好吧,有問題的方法將實施工會。因此,這:
Set union(Set x, Set y) {
String[] newtemparray = new String[x.getLength];
newtemparray = x.getMember;
return x;
}
我得到這個錯誤:
symbol : variable getLength location: class Set String[] newtemparray = new String[x.getLength]; ^ d:\javaprojects\Set.java:122: cannot find symbol symbol : variable getMember location: class Set newtemparray = x.getMember; ^ 2 errors
我對聯盟的做法是:
- 創建對象x長度的字符串大小的臨時數組。
- 通過循環對象和調用getMember將對象x成員存儲到臨時數組
- 循環對象y成員並檢查對臨時數組是否存在元素。
- 丟棄是否存在/添加,如果它不存在
- 返回obj x與聯合數組。
不直接關係到你的錯誤之類的兩種方法,但你創造了聯盟的方法失敗,除非'Y'是'X'的一個子集。這是因爲你創建的數組的大小是'X'的大小。而不是「如果它存在/丟棄,如果它不存在則添加」,只是「如果存在則丟棄」。這樣數組就是'X'的所有元素而不是'Y',它必須<= X的大小,所以沒有長度問題。然後返回一個由'Y'創建的'Set'與臨時數組。 – Phil 2010-05-19 04:16:01