I am trying to build a Set for myself, with just intersect,union and print as the methods.
my test code is
public static void main(String[] args)
{
double[] a = {1,2,3,4,101.9};
double[] b = {3,7,13,901,-29.1,0.0001};
NumSet test;
test = new NumSet(a);
test.intersect(a, b);
System.out.println("tried intersect [1,2,3,101.9], [3,7,13,901,-28.8,0.001]");
test.print();
System.out.println("tried test.print()");
test.union(a,b);
System.out.println("tried union [1,3,3,5,7,9], [3,5,7,13]");
}
which gives me an
tried intersect [1,2,3,101.9], [3,7,13,901,-28.8,0.001]
0.0,0.0,0.0,out of bounds 4 >= 4 or 4 < 0
0.0tried test.print()
tried union [1,3,3,5,7,9], [3,5,7,13]
which is strange, because my print has nothing to do with printing such a sentence as the second one.
public void print()
{
for (int i = 0; i < set.size() - 1; i++)
{
System.out.print(set.lookup(i) + ",");
}
System.out.print(set.lookup(set.size()));
}
this is my print method. and as the class is extending another class,
public class NumSet extends NumArrayList
{
NumList set;
public NumSet(double[] sth)
{
set = new NumArrayList();
for (int i = 0; i < sth.length; i++)
{
set.insert(i, sth[i]);
}
set.removeDuplicates();
}
int numSet = 0;
public NumList intersect(double[] S1, double[] S2)
{
for (int i = 0; i < S1.length; i++)
{
for(int j = 0; j < S2.length; j++)
{
if (S1[i] == S2[j])
{
set.insert(numSet, S1[i]);
numSet++;
}
}
}
set.removeDuplicates();
return set;
}
public NumList union(double[] S1, double[] S2)
{
for (int i = 0; i < S1.length; i++)
{
set.insert(1, S1[i]);
}
for (int i = 0; i < S2.length; i++)
{
set.insert(1, S2[i]);
}
set.removeDuplicates();
return set;
}
I went and checked the print method there.
public void print()
{
for (int i = 0; i < numItems - 1; i++)
{
System.out.println(items[i]+ ",");
}
System.out.println(items[numItems-1]);
}
which leaves me dumbfounded as to why my error message is as such.
Where is my print referring to to get such a weird message?
You're missing a -1
in your last print
statement:
System.out.print(set.lookup(set.size()));
should be:
System.out.print(set.lookup(set.size() - 1));
Remember that lookup
indexes are zero-based, so using set.size()
is indeed out of bounds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.