Java代码,数组/矩阵组件

时间:2014-04-03 22:34:46

标签: java arrays

我在java中有这个3x1数组/矩阵。我如何使用数组中的任何一个组件。让我们说我想

r = Math.pow(second row component,2) + Math.pow(third row component,2)

组件的调用代码是什么? 感谢

3 个答案:

答案 0 :(得分:0)

使用foreach迭代数组,然后按索引

添加Math.Pow索引
double[] myArray = { 1.2, 16.5, 20.0 }
double r = 0;
for(double d : myArray)
{
    r+= Math.pow(d,2);
}

答案 1 :(得分:0)

如果您的组件是elements,那么语法如下:

int[] numbers = {1,2,3,4,5};

int num = numbers[0];

System.out.println("Number: " + num);

// Outputs Number: 1

额外阅读

您应该阅读documentation。这包含了在Java中使用数组类型所需的所有信息。

答案 2 :(得分:0)

如果要访问数组的元素,请在括号中附加元素的索引:

r = Math.pow(second row component,2) + Math.pow(third row component,2)

变为

r = Math.pow(arrayVariable[1],2) + Math.pow(arrayVariable[2],2)

请记住,数组索引是零基础。