如何打印数组的单个指定元素?

时间:2019-05-14 07:47:14

标签: java arrays

使用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 个答案:

答案 0 :(得分:0)

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]);
    }
}

答案 1 :(得分:0)

您不需要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++) {
        System.out.println(a[j] + " ");
    }

    int index = 20;
    System.out.println(index + " = " + a[index]);
    }
}

答案 2 :(得分:0)

public static void main(String[] args) {

    /* Declaring Array */
    char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray(); // alphabets 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( alpha[i]+" = "x + " "); // value for corresponding alphabets
}
}
}

您需要做的是声明字母array char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray();System.out.print( alpha[i+1]+" = "x + " ");

希望这会有所帮助。快乐编码<3!

谢谢