返回新对象,用Java覆盖现有对象

时间:2010-05-20 09:21:55

标签: java object

注意:这是一项任务。

您好,

好的我有这个方法可以创建一个2组的联合。

mport java.io.*;

class Set {

       public int numberOfElements;
       public String[] setElements; 
       public int maxNumberOfElements;

       // constructor for our Set class   

       public Set(int numberOfE, int setE, int maxNumberOfE) {
          this.numberOfElements    = numberOfE;
          this.setElements    = new String[setE];
          this.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] != null && 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++) {
               if (setElements[i] != null) {
                    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[] newXtemparray = new String[x.getLength()];
          String[] newYtemparray = new String[y.getLength()];
          int len = newYtemparray.length + newXtemparray.length;
          Set temp = new Set(0,len,len);
          newXtemparray = x.getMember();
          newYtemparray = x.getMember();
          for(int i = 0; i < newYtemparray.length; i++) {
               temp.add(newYtemparray[i]);
          }

          for(int j = 0; j < newXtemparray.length; j++) {
               temp.add(newXtemparray[j]);
          }
          return temp;
       }

     Set difference(Set x, Set y) {
          String[] newXtemparray = new String[x.getLength()];
          String[] newYtemparray = new String[y.getLength()];
          int len = newYtemparray.length + newXtemparray.length;
          Set temp = new Set(0,len,len);
          newXtemparray = x.getMember();
          newYtemparray = x.getMember();
          for(int i = 0; i < newXtemparray.length; i++) {
               temp.add(newYtemparray[i]);
          }

          for(int j = 0; j < newYtemparray.length; j++) {
               int retval = temp.findElement(newYtemparray[j]);
               if( retval != -1 ) {
                  temp.remove(newYtemparray[j]);
               }
          }
          return temp;
       }
}

// 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(0,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 sixb 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);
     setB.add("b");
     setB.add("z");
     setB.add("x");
     setB.add("y");
     Set setC = setA.union(setB,setA);
     System.out.println("Elements of setA");
     setA.printSet();
     System.out.println("Union of setA and setB");
     setC.printSet();
     }
}

union方法的作用是某种意义上我可以在某种程度上调用另一个方法但它不能完成这项工作,我认为它会创建setA和setB的所有元素并将它们联合起来,但它只返回setB的元素。样本输出如下:

java SetDemo
Enter string element to be added
hello
You entered hello
Set size is: 10
Set is empty? false
Element sixb is member of setA? -1
Set is full? true
Element b located on index: 2
Set size is: 9
Elements of setA
Member elements on index: 0 hello
Member elements on index: 1 world
Member elements on index: 2 six
Member elements on index: 3 seven
Union of setA and setB
Member elements on index: 0 b
Member elements on index: 1 z
Member elements on index: 2 x
Member elements on index: 3 y

感谢, 羽扇豆

1 个答案:

答案 0 :(得分:2)

union()中,您有错字:

newYtemparray = x.getMember();

应该是

newYtemparray = y.getMember();