【发布时间】:2010-03-31 01:12:53
【问题描述】:
我有这个函数,它返回一个数据类型 InetAddress[]
public InetAddress []
lookupAllHostAddr(String host) throws UnknownHostException {
Name name = null;
try {
name = new Name(host);
}
catch (TextParseException e) {
throw new UnknownHostException(host);
}
Record [] records = null;
if (preferV6)
records = new Lookup(name, Type.AAAA).run();
if (records == null)
records = new Lookup(name, Type.A).run();
if (records == null && !preferV6)
records = new Lookup(name, Type.AAAA).run();
if (records == null)
throw new UnknownHostException(host);
InetAddress[] array = new InetAddress[records.length];
for (int i = 0; i < records.length; i++) {
Record record = records[i];
if (records[i] instanceof ARecord) {
ARecord a = (ARecord) records[i];
array[i] = a.getAddress();
} else {
AAAARecord aaaa = (AAAARecord) records[i];
array[i] = aaaa.getAddress();
}
}
return array;
}
Eclipse 抱怨返回类型应该是 byte[][] 但是当我将返回类型更改为 byte[][] 时,它抱怨该函数返回了错误的数据类型。我陷入了一个循环。有谁知道这里发生了什么?
【问题讨论】:
-
首先你应该学会使用集合类而不是原始数组。其次,您应该学会为每个构造使用新的,而不是使用计数器迭代数组,充其量是一次性错误。