gpt4 book ai didi

java - Java中两个集合的笛卡尔积

转载 作者:行者123 更新时间:2023-12-01 21:27:04 24 4
gpt4 key购买 nike

我正在尝试解决我正在使用的教科书中的一个问题,该问题与笛卡尔积和集合有关,而不使用内置的 java API 或任何奇特的函数。

例如集合 A 包含 = {1,3,4}

集合 B 包含 = {2,5}

他们的产品将产生此结果{(1,2),(1,5),(3,2),(3,5),(4,2),(4,5)}

我已经编写了一些方法来在每组上执行各种功能,但这是我想到的。我怎样才能将其应用到集合中?

public String cartesian(Set other)
{
String result = "";
int res;

for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("@@@@@"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];

}
}

return result;

}

该方法以字符串形式返回结果。我的逻辑是同时完成每个集合的元素,但我一直在思考如何将它们交叉在一起。

这是我的其余代码。

 public class Sets {
public static void main(String[] args)
{

Set set1;
set1 = new Set();


Set set2 = new Set();

set1.add(1);
set1.add(2);
set1.add(3);

set2.add(3);
set2.add(4);
/*set2.add(4);
set2.add(5);*/

//System.out.println(set1.difference(set2));

System.out.println(set1.cartesianReformed(set2));



}
}

用户定义的Set

class Set
{
private int[] data;
private int size;

public Set()
{
data = new int[20];
size = 0;

}

public void add(int value)
{
int[] copy;

//avoiding duplicates
if ( !in(value) )
{
if ( size > data.length )
{
copy = new int[data.length * 2];

System.arraycopy(data, 0, copy,0,data.length);

data = copy;
}
data[size] = value;

size++;
}
else
{
System.out.println("You are trying to insert a number that's already here ---> " + value);
}

}

public String toString()
{
String result = "{";
for(int i = 0; i < size; i++)
{
result += "" + data[i];
//Add a comma after all but the last item
if ( i < size - 1 )
{
result += ",";
}

}
result += "}";
return result;
}

public boolean in(int value)
{
boolean result = false;

for(int i = 0; i < size; i++)
{
if ( data[i] == value )
{
result = true;
}
}

return result;
}

public Set intersection(Set other)
{
Set result = new Set();

for ( int i = 0; i < size; ++i )
{
if ( other.in(data[i]) )
{
result.add(data[i]);
}
}
return result;
}

public boolean equals(Set other)
{
boolean result = false;

int count = 0;

for ( int i = 0; i < size; ++i ) //iterating over this
{
if ( other.in(data[i]) )
{
count++;
}

if ( count == size )
{
result = true;
}
}
return result;
}

public Set difference(Set other)
{
Set result = new Set();

for(int i = 0; i < size; ++i)
{
if ( !other.in(data[i]) )
{
result.add(data[i]);
}
}

return result;
}



public String cartesian(Set other)
{
String result = "";
int res;

for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("@@@@@"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];

}
}

return result;

}

public Set union(Set other) {
Set result = (Set)other.clone();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}

public Object clone() {
Set result = new Set();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}
}

最佳答案

这样的事情会起作用:

public String cartesian (Set other)
{
String [] cart = new String [this.size * other.size];

int k = 0;
for (int i : this.data)
{
for (int j : other.data)
{
cart[k++] = "(" + i + "," + j + ")";
}
}

return Arrays.toString(cart);
}

返回:

[(1,2), (1,5), (3,2), (3,5), (4,2), (4,5)]

注意:

  • 将类命名为 Set 是一个坏主意,因为它与 java.util.Set 冲突。

关于java - Java中两个集合的笛卡尔积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958331/

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