gpt4 book ai didi

java - 返回新对象,覆盖Java中现有的对象

转载 作者:行者123 更新时间:2023-12-02 08:32:45 24 4
gpt4 key购买 nike

注意:这是一项作业。

嗨,

好吧,我有这个方法,可以创建 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

谢谢,羽扇 bean

最佳答案

union()中,这里有一个拼写错误:

newYtemparray = x.getMember();

应该是

newYtemparray = y.getMember();

关于java - 返回新对象,覆盖Java中现有的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2872424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com