I have the following error in compiler
return ans;
^
required: byte[]
found: int[]
我有这种方法,我想返回一种字节。任何人都可以告诉我如何投射ans以使其与我的方法类型
兼容public byte[] element(int m) {
//if (q!= a.length)
// throw new Exception("Array length does not equal k");
int f;
int q;
int[] t;
this.first = f;
this.second = q;
this.data = new int[q];
for (int i = 0; i < t.length; ++i) {
this.data[i] = t[i];
}
// if (!this.IsValid())
// throw new Exception("Bad value from array");
int ans[] = new int[this.second];
int a = this.first;
int b = this.second;
int x = (choose(this.first,this.second) - 1) - m; // x is the "dual" of m
for (int i = 0; i < this.second; ++i) {
ans[i] = largestV(a, b, x); // largest value v, where v < a and vCb < x
x = x - choose(ans[i], b);
a = ans[i];
b = b - 1;
}
for (int i = 0; i < this.second; ++i) {
ans[i] = (first-1) - ans[i];
return ans;
}
}
注意:我希望它返回字节类型而不是int但我试图转换ans但没有成功。
答案 0 :(得分:0)
你必须将int [] ans更改为Byte [] ans,你试图返回一个int数组,但你必须返回你的字节数组
int f;
int q;
int [] t;
this.first = f;
this.second = q;
this.data = new int[q];
for (int i = 0; i < t.length; ++i)
this.data[i] = t[i];
// if (!this.IsValid())
// throw new Exception("Bad value from array");
byte ans []= new byte[this.second];
int a = this.first;
int b = this.second;
int x = (choose(this.first,this.second) - 1) - m; // x is the "dual" of m
for (int i = 0; i < this.second; ++i) {
ans[i]= largestV(a, b, x); // largest value v, where v < a and vCb < x
x = x - choose(ans[i], b);
a = ans[i];
b = b - 1;
}
for (int i = 0; i < this.second; ++i)
ans[i] = (first-1) - ans[i];
return ans;
}
答案 1 :(得分:0)
Dunno为什么你想要int
工作并返回byte
。除了数据之外,您可以创建一个与ans
大小相同的新字节数组,并在最后一个循环中执行此操作
for (int i = 0; i < this.second; ++i)
{
ans[i] = (first-1) - ans[i]);
ans_byte_version[i] = (byte) ((first-1) - ans[i]);
}
return ans_byte_version;
然而,如果你告诉我们你想做什么,那会更好。为什么在首先想要字节时声明为int。