使用基本的java数组实现Set的并集

发布于 2024-09-01 22:10:01 字数 6326 浏览 4 评论 0原文

注意:这是一项作业。

继续使用 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);
     }
}

好的,有问题的方法将是 union 的实现。因此:

Set union(Set x, Set y) {
          String[] newtemparray = new String[x.getLength];
          newtemparray = x.getMember;
          return x;
       }

我收到此错误:

symbol:变量 getLength
地点:班级集
          String[] newtemparray = new String[x.getLength];
                                              ^
d:\javaprojects\Set.java:122: 找不到符号
符号:变量 getMember
地点:班级集
          newtemparray = x.getMember;
                          ^
2 个错误

我的联合方法是:

  • 创建临时字符串数组,其大小为对象 x 长度。
  • 通过循环对象并调用 getMember
  • 循环对象 y 成员,将对象 x 成员存储到临时数组,并检查临时数组中是否存在元素。
  • 如果存在则丢弃/如果不存在则添加,
  • 返回 obj x 和联合数组。

Note: This is an assignment.

Continuing with my Set implementation using Java basic array, I'm now struggling with the 3 to last function namely the 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);
     }
}

Ok the method in question will be the implementation of union. As such this:

Set union(Set x, Set y) {
          String[] newtemparray = new String[x.getLength];
          newtemparray = x.getMember;
          return x;
       }

I got this error:

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

My approach for union would be:

  • create temporary array of string with size of the object x length.
  • store object x members to temporary array by looping the object and calling the getMember
  • loop object y members and check if element exist against temporary array.
  • discard if it exist/add if it is not there
  • return obj x with the union array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-09-08 22:10:01

分别是 x.getLength()x.getMember()

getLength()getMember() 都是 Set 类的方法,因此您需要 () 因为你正在调用一个方法

It's x.getLength() and x.getMember(), respectively.

getLength() and getMember() are both methods of the class Set and hence you need the () because you are invoking a method

夜清冷一曲。 2024-09-08 22:10:01

您遇到的问题是 x.getLength 是一个函数,而不是一个成员。您需要改为调用 x.getLength() 。

The problem you are having is that x.getLength is a function, not a member. You need to call x.getLength() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文