使用Array.getDouble()方法我可以打印整个数组,但我无法弄清楚如何只打印一个元素。即。如果我只想打印索引20,我该怎么做?import java.lang.reflect.Array;public class freq {
public static void main(String[] args) {
/* Declaring Array */
/** English letter frequencies */
double a[] = {
0.0855, 0.0160, 0.0316, 0.0387, 0.1210,
0.0218, 0.0209, 0.0496, 0.0733, 0.0022,
0.0081, 0.0421, 0.0253, 0.0717, 0.0747,
0.0207, 0.0010, 0.0633, 0.0673, 0.0894,
0.0268, 0.0106, 0.0183, 0.0019, 0.0172,
0.0011
};
/* Traversing the array */
for (int j = 0; j < 26; j++) {
/* Array.getDouble() Method */
double x = (double)Array.getDouble(a, j);
/* Print Values */
System.out.print(x + " ");
}
}}我得到输出:0.0855 0.0160 0.0316 0.0387 0.1210 0.0218 0.0209 0.0496 0.0733 0.0022 0.0081 0.0421 0.0253 0.0717 0.0747 0.0207 0.0010 0.0633 0.0673 0.0894 0.0268 0.0106 0.0183 0.0019 0.0172 0.0011我想获得(例如)c = 0.0316的输出
3 回答

qq_笑_17
TA贡献1818条经验 获得超7个赞
import java.lang.reflect.Array;public class freq { public static void main(String[] args) { double a[] = { 0.0855, 0.0160, 0.0316, 0.0387, 0.1210, 0.0218, 0.0209, 0.0496, 0.0733, 0.0022, 0.0081, 0.0421, 0.0253, 0.0717, 0.0747, 0.0207, 0.0010, 0.0633, 0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011 }; System.out.println("c = " + a[2]); }}

ibeautiful
TA贡献1993条经验 获得超5个赞
你不需要数组调用。您可以像这样简化它:
public class Freq { public static void main(String[] args) { /* Declaring Array */ /** English letter frequencies */ double a[] = { 0.0855, 0.0160, 0.0316, 0.0387, 0.1210, 0.0218, 0.0209, 0.0496, 0.0733, 0.0022, 0.0081, 0.0421, 0.0253, 0.0717, 0.0747, 0.0207, 0.0010, 0.0633, 0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011 }; /* Traversing the array */ for (int j = 0; j < 26; j++) { System.out.println(a[j] + " "); } int index = 20; System.out.println(index + " = " + a[index]); }}
添加回答
举报
0/150
提交
取消