我正在尝试从我正在使用的教科书中解决问题,这与笛卡尔产品和集合有关,而不使用内置的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;
}
}
答案 0 :(得分:2)
这样的事情会起作用:
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)]
注意:
答案 1 :(得分:1)
让我们谈谈问题的关键。您基本上需要2个列表中的所有元素组合。以下是一种非常简单的方法,通过使用嵌套的for循环遍历集合A和B中的元素。
Set<Integer> A = new HashSet<Integer>();
Set<Integer> B = new HashSet<Integer>();
for(int i = 1 ; i < 5 ; i++)
A.add(i);
for(int i = 10 ; i < 13 ; i++)
B.add(i);
System.out.println("A: " + A);
System.out.println("B: " + B);
List<Set<Integer>> list = new ArrayList<Set<Integer>>();
for(Integer i: A) {
for(Integer j: B) {
Set<Integer> combination = new HashSet<Integer>();
combination.add(i);
combination.add(j);
list.add(combination);
}
}
System.out.println(list);