亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何打印數組的單個指定元素?

如何打印數組的單個指定元素?

元芳怎么了 2019-04-17 14:15:52
使用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]);
    }}


查看完整回答
反對 回復 2019-05-15
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

你不需要數組調用。您可以像這樣簡化它:

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


查看完整回答
反對 回復 2019-05-15
  • 3 回答
  • 0 關注
  • 1013 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號