本文介绍了Java(内置方法)的实现中的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,新年快乐.

我来自 matlab 的世界,不到一年就开始用 java 编写代码.我正在解决可能很容易但请耐心等待的问题!这是matlab的代码:state 是 3d 数组的变量,其大小为:2x4x64.状态值是:(这是我在matlab编辑器上得到的)

state(:,:,1) =1 0 0 02 0 1 1状态(:,:,2) =3 0 1 04 0 0 1状态(:,:,3) =5 0 1 16 0 0 0状态(:,:,4) =7 0 0 18 0 1 0状态(:,:,5) =9 0 1 110 0 0 0状态(:,:,6) =11 0 0 112 0 1 0状态(:,:,7) =13 0 0 014 0 1 1状态(:,:,8) =15 0 1 016 0 0 1状态(:,:,9) =17 0 0 018 0 1 1状态(:,:,10) =19 0 1 020 0 0 1状态(:,:,11) =21 0 1 122 0 0 0状态(:,:,12) =23 0 0 124 0 1 0状态(:,:,13) =25 0 1 126 0 0 0状态(:,:,14) =27 0 0 128 0 1 0状态(:,:,15) =29 0 0 030 0 1 1状态(:,:,16) =31 0 1 032 0 0 1状态(:,:,17) =33 0 0 134 0 1 0状态(:,:,18) =35 0 1 136 0 0 0状态(:,:,19) =37 0 1 038 0 0 1状态(:,:,20) =39 0 0 040 0 1 1状态(:,:,21) =41 0 1 042 0 0 1状态(:,:,22) =43 0 0 044 0 1 1状态(:,:,23) =45 0 0 146 0 1 0状态(:,:,24) =47 0 1 148 0 0 0状态(:,:,25) =49 0 0 150 0 1 0状态(:,:,26) =51 0 1 152 0 0 0状态(:,:,27) =53 0 1 054 0 0 1状态(:,:,28) =55 0 0 056 0 1 1状态(:,:,29) =57 0 1 058 0 0 1状态(:,:,30) =59 0 0 060 0 1 1状态(:,:,31) =61 0 0 162 0 1 0状态(:,:,32) =63 0 1 164 0 0 0状态(:,:,33) =1 1 1 12 1 0 0状态(:,:,34) =3 1 0 14 1 1 0状态(:,:,35) =5 1 0 06 1 1 1状态(:,:,36) =7 1 1 08 1 0 1状态(:,:,37) =9 1 0 010 1 1 1状态(:,:,38) =11 1 1 012 1 0 1状态(:,:,39) =13 1 1 114 1 0 0状态(:,:,40) =15 1 0 116 1 1 0状态(:,:,41) =17 1 1 118 1 0 0状态(:,:,42) =19 1 0 120 1 1 0状态(:,:,43) =21 1 0 022 1 1 1状态(:,:,44) =23 1 1 024 1 0 1状态(:,:,45) =25 1 0 026 1 1 1状态(:,:,46) =27 1 1 028 1 0 1状态(:,:,47) =29 1 1 130 1 0 0状态(:,:,48) =31 1 0 132 1 1 0状态(:,:,49) =33 1 1 034 1 0 1状态(:,:,50) =35 1 0 036 1 1 1状态(:,:,51) =37 1 0 138 1 1 0状态(:,:,52) =39 1 1 140 1 0 0状态(:,:,53) =41 1 0 142 1 1 0状态(:,:,54) =43 1 1 144 1 0 0状态(:,:,55) =45 1 1 046 1 0 1状态(:,:,56) =47 1 0 048 1 1 1状态(:,:,57) =49 1 1 050 1 0 1状态(:,:,58) =51 1 0 052 1 1 1状态(:,:,59) =53 1 0 154 1 1 0状态(:,:,60) =55 1 1 156 1 0 0状态(:,:,61) =57 1 0 158 1 1 0状态(:,:,62) =59 1 1 160 1 0 0状态(:,:,63) =61 1 1 062 1 0 1状态(:,:,64) =63 1 0 064 1 1 1%%%%%%%%%%%%%%%%%%%%显示 =[1 0];tmp=[0,0];对于 m=1:2tmp[1]=sum(abs(state(l, 3:4 ,j)-revealed)) % 这里 state(m, 3:4 ,j) 实际上是第 3 和第 4 列从它们中减去在每个给定 j 的行 (m=1, 2).结尾所以最后它返回 tmp.

谁不理解 matlab 是有一个大小为 2x4x64 的数组 state 和 state(l, 3:4 ,j) 根据 j 给我两列值,然后我们减去从那时显示的数组和我们之后做 abs 然后总结到数组结果.例如:状态 (m, 3:4 , 1) .J=1 给我 [0 0] 和 [1 1] 对于 m=1 , m=2 在第三和第四列.所以我每减去一米就透露[1 0] 从第三列的两个值和第 4 列的值根据 m 得出.所以这里在 m=1 我们有 [0 0] 所以 [0 0] - 显示 =>[0 0]-[1 0] =>[-1 0] 我们做 abs([-1 0]) 所以我们得到 [1 0] 然后我们求和它是 1+0 =>1.在 m=1 时,我们得到 [1 1] 所以 [1 1]-[1 0] =>[1 0] 我们在这个数组上做 abs =>[1 0] 然后我们做 sum 1+0 =>1.

我正在尝试在 java 中做同样的事情/同样的概念(将此代码 matlab 转换为 java).所以我所做的是:

int[64][2][4] state={{{ 1 , 0 , 0 , 0},{ 2 , 0 , 1, 1 }},{{ 3 , 0 , 1 , 0},{4 , 0 , 0 , 1}},{{ 5 , 0 , 1 , 1},{6 , 0 , 0 , 0}},{{ 7 , 0 , 0 , 1},{ 8 , 0 , 1 , 0 }}{ {{9 ,0 , 1 , 1},{10 , 0 , 0 , 0}},{ { 11 , 0 , 0 , 1},{12 , 0 , 1 , 0}},{ { 13 , 0 , 0 , 0},{14 , 0 , 1 , 1}},{ { { 15 , 0 , 1 , 0},{ 16 , 0 , 0 , 1} },{{17 , 0 , 0 , 0},{18 , 0 , 1 , 1 }},{{19 , 0 , 1 , 0},{20 , 0 , 0 , 1}},{{21 , 0 , 1 , 1},{ 22 , 0 , 0 , 0}},{ { 23 , 0 , 0, 1} ,{24 , 0 , 1 , 0}},{ {{25 , 0 , 1 , 1},{26 , 0 , 0 , 0}},{ {27 , 0 , 0 , 1},{28 , 0 , 1 , 0}},{{29 , 0 , 0 , 0},{30 , 0 , 1 , 1}},{{ 31 , 0 , 1 , 0},{32 , 0 , 0 , 1}},{ { 33 , 0 , 0 , 1},{34 , 0 , 1 , 0} },{{35 , 0 , 1 , 1},{36 , 0 , 0 , 0}},{{ 37 , 0 , 1 , 0},{38 , 0 , 0 , 1}},{{39 , 0 , 0 , 0},{ 40 , 0 , 1 , 1}},{{41 , 0 , 1 , 0},{ 42 , 0 , 0 , 1}},{{ 43 , 0 , 0 , 0},{ 44 , 0 , 1 , 1}}{45 , 0 , 0 , 1},{46 , 0 , 1 , 0} },{ { 47 , 0 , 1 , 1 },{ 48 , 0 , 0 , 0 } },{{ 49 , 0 , 0 , 1 },{ 50 , 0 , 1 , 0 }},{ { 51 , 0 , 1 , 1 },{52 , 0 , 0 , 0} },{ { 53 , 0, 1 , 0 },{54 , 0 , 0 , 1} },{ { 55 , 0 , 0 , 0 },{ 56 , 0 , 1 , 1} },{ {57 , 0 , 1 , 0},{58 , 0 , 0 , 1} },{ { 59 , 0 , 0 , 0 },{ 60 , 0 , 1 , 1} },{ { { 61 , 0 , 0 , 1 },{ 62 , 0 , 1 , 0} },{ { 63 , 0 , 1 , 1},{ 64 , 0 , 0 , 0 } },};公开课 GlobalMember{int[] 显示 =[1 0];int[] tmp={0,0};for(int j=0 ; j

但听起来我遗漏了一些东西并且根本无法编译,任何人都可以帮助我我如何在java中实现这段代码matlab?我的意思是在java中实现相同的matlab代码概念.

我在这个问题上挣扎了大约一个星期,但我不明白问题究竟出在哪里.

非常感谢.

解决方案

首先让我们熟悉基础知识.让我们从二维数组开始.

a) 二维数组

想象一下二维数组不是数字网格,而是一维数组的层,因为这些层中的每一层都可以有任意长度的一维数组,让我们看一个例子

//这个二维数组有3层,每一层用{}括号表示int[][] 测试={{1,2}//第0层有2个元素,{3,4,5}//第1层有3个元素,{6,7,8,9}//第2层有4个元素};

要查询此代码,您可以这样做

int layers=test.length;//这给了我们二维数组中的总层数System.out.println(layers);for(int i=0;i<layers;i++){System.out.println(test[i].length);}//这个查询每一层的元素个数

输出:

3//层数=32//第0层有2个元素3//第1层有3个元素4//第2层有4个元素

你也可以像这样初始化一个二维数组

int rows=2;int cols=3;int[][] test=new int[rows][cols];

然而,一个限制是您可能已经从上面猜到了,您只能以网格方式初始化二维数组.层数为行,每层中每个一维数组的长度为列.

要遍历每个元素,您可以这样做

for(int layer=0;layer

b) 3D 阵列

现在就像二维数组包含多层一维数组一样,3D数组包含多层二维数组,我们来看一个例子

 int[][][] test={{//这是第0层{0,1},{2,3,4}},{//这是第1层{5,6},{7,8,9},{10,11,12}},{//这是第2层{13,14},{15,16,17},{18,19,20,21},{22,23,24,25,26}}};

同样,你也可以像这样初始化一个 3D 数组

int layer=3;int行=4;int col=5;int[][][] test=new int[layers][rows][col];

再次限制每个二维数组层必须以网格方式初始化

事实上,你可以像这样从这个 3D 数组中的任何层引用一个 2D 数组

int[][] test2=test[0];//获取存储在第0层的二维数组

还要遍历这个 3D 数组中的每个元素,你已经挖掘了所有层

for(int layer2D=0;layer2D<test.length;layer2D++)//遍历这个3D数组中的每一个2D数组layer{for(int layer1D=0;layer1D<test[layer2D].length;layer1D++)//迭代当前二维数组的每一层{for(int element=0;element<test[layer2D][layer1D].length;element++)//最终遍历当前一维图层或一维数组的每个元素{int value=test[layer2D][layer1D][element];}}}

好吧,让我们现在解决您的问题.

1) 修复您的阵列

由于您使用方法一初始化 3D 数组,因此您无需指定长度.这是你的最终数组

int[][][] state=//无需指定长度只需使用空括号{{//第0层{1 , 0 , 0 , 0},{2, 0, 1, 1 }},{//第1层等等{3 , 0 , 1 , 0},{4, 0, 0, 1}},{{5 , 0 , 1 , 1},{6 , 0 , 0 , 0}},{{7 , 0 , 0 , 1},{8 , 0 , 1 , 0 }},{{9 ,0 , 1 , 1},{10 , 0 , 0 , 0}},{{11 , 0 , 0 , 1},{12 , 0 , 1 , 0}},{{13 , 0 , 0 , 0},{14 , 0 , 1 , 1}},{{15 , 0 , 1 , 0},{16 , 0 , 0 , 1}},{{17 , 0 , 0 , 0},{18 , 0 , 1 , 1 }},{{19 , 0 , 1 , 0},{20 , 0 , 0 , 1}},{{21 , 0 , 1 , 1},{22 , 0 , 0 , 0}},{{23, 0, 0, 1},{24 , 0 , 1 , 0}},{{25 , 0 , 1 , 1},{26 , 0 , 0 , 0}},{{27 , 0 , 0 , 1},{28 , 0 , 1 , 0}},{{29 , 0 , 0 , 0},{30 , 0 , 1 , 1}},{{31 , 0 , 1 , 0},{32 , 0 , 0 , 1}},{{33 , 0 , 0 , 1},{34 , 0 , 1 , 0}},{{35 , 0 , 1 , 1},{36 , 0 , 0 , 0}},{{37 , 0 , 1 , 0},{38 , 0 , 0 , 1}},{{39 , 0 , 0 , 0},{40 , 0 , 1 , 1}},{{41 , 0 , 1 , 0},{42 , 0 , 0 , 1}},{{43 , 0 , 0 , 0},{44 , 0 , 1 , 1}},{{45 , 0 , 0 , 1},{46 , 0 , 1 , 0}},{{47 , 0 , 1 , 1 },{48 , 0 , 0 , 0 }},{{ 49 , 0 , 0 , 1 },{ 50 , 0 , 1 , 0 }},{{51 , 0 , 1 , 1 },{52 , 0 , 0 , 0}},{{53 , 0, 1 , 0 },{54, 0, 0, 1}},{{55 , 0 , 0 , 0 },{56 , 0 , 1 , 1}},{{57 , 0 , 1 , 0},{58 , 0 , 0 , 1}},{{59 , 0 , 0 , 0 },{60 , 0 , 1 , 1}},{{61 , 0 , 0 , 1 },{62 , 0 , 1 , 0}},{{63 , 0 , 1 , 1},{64 , 0 , 0 , 0 }}};
  1. 修复您的代码

这里的代码

公共类 GlobalMember{int[] 显示 =[1 0];int[] tmp={0,0};for(int j=0 ; j

你不会看到java中的每个程序都以一个类和一个main方法开始,所以你所做的一切都需要封装在一个方法中,这个方法位于类内部,你可以阅读这个简单的教程

让我们把它放在一个方法中,如果我不理解你的逻辑,请原谅我,但基本上如果你试图找到所有元素的总和,你必须声明一个变量来存储结果并继续添加它以获得你的结果.在此期间,让我们在此类中也声明您的主要方法

公共类 FindSum{public static void main(String[] args){int[][][] state=//我上面显示的数组因为我不能被要求再次粘贴到这里System.out.println(findSum(state));//你在这里调用那个函数}public static int findSum(int[][][] state){int sum=0;//使用与上面相同的逻辑for(int layer2D=0;layer2D

我在这里只介绍了基础知识,因此可以找到一些很好的教程,那里有很多教程.

Hello and Happy new Year to all guys.

I came from world of matlab, and before one year started to code in java.I'm in countering problem that might be easy but bear with me please!here's the code of matlab:state is a variable of 3d array which its size: 2x4x64.state value is:(that's what I get on the editor of matlab)

state(:,:,1) =

     1     0     0     0
     2     0     1     1


state(:,:,2) =

     3     0     1     0
     4     0     0     1


state(:,:,3) =

     5     0     1     1
     6     0     0     0


state(:,:,4) =

     7     0     0     1
     8     0     1     0


state(:,:,5) =

     9     0     1     1
    10     0     0     0


state(:,:,6) =

    11     0     0     1
    12     0     1     0


state(:,:,7) =

    13     0     0     0
    14     0     1     1


state(:,:,8) =

    15     0     1     0
    16     0     0     1


state(:,:,9) =

    17     0     0     0
    18     0     1     1


state(:,:,10) =

    19     0     1     0
    20     0     0     1


state(:,:,11) =

    21     0     1     1
    22     0     0     0


state(:,:,12) =

    23     0     0     1
    24     0     1     0


state(:,:,13) =

    25     0     1     1
    26     0     0     0


state(:,:,14) =

    27     0     0     1
    28     0     1     0


state(:,:,15) =

    29     0     0     0
    30     0     1     1


state(:,:,16) =

    31     0     1     0
    32     0     0     1


state(:,:,17) =

    33     0     0     1
    34     0     1     0


state(:,:,18) =

    35     0     1     1
    36     0     0     0


state(:,:,19) =

    37     0     1     0
    38     0     0     1


state(:,:,20) =

    39     0     0     0
    40     0     1     1


state(:,:,21) =

    41     0     1     0
    42     0     0     1


state(:,:,22) =

    43     0     0     0
    44     0     1     1


state(:,:,23) =

    45     0     0     1
    46     0     1     0


state(:,:,24) =

    47     0     1     1
    48     0     0     0


state(:,:,25) =

    49     0     0     1
    50     0     1     0

state(:,:,26) =

    51     0     1     1
    52     0     0     0


state(:,:,27) =

    53     0     1     0
    54     0     0     1


state(:,:,28) =

    55     0     0     0
    56     0     1     1


state(:,:,29) =

    57     0     1     0
    58     0     0     1


state(:,:,30) =

    59     0     0     0
    60     0     1     1


state(:,:,31) =

    61     0     0     1
    62     0     1     0


state(:,:,32) =

    63     0     1     1
    64     0     0     0


state(:,:,33) =

     1     1     1     1
     2     1     0     0


state(:,:,34) =

     3     1     0     1
     4     1     1     0


state(:,:,35) =

     5     1     0     0
     6     1     1     1


state(:,:,36) =

     7     1     1     0
     8     1     0     1


state(:,:,37) =

     9     1     0     0
    10     1     1     1


state(:,:,38) =

    11     1     1     0
    12     1     0     1


state(:,:,39) =

    13     1     1     1
    14     1     0     0


state(:,:,40) =

    15     1     0     1
    16     1     1     0


state(:,:,41) =

    17     1     1     1
    18     1     0     0


state(:,:,42) =

    19     1     0     1
    20     1     1     0


state(:,:,43) =

    21     1     0     0
    22     1     1     1


state(:,:,44) =

    23     1     1     0
    24     1     0     1


state(:,:,45) =

    25     1     0     0
    26     1     1     1


state(:,:,46) =

    27     1     1     0
    28     1     0     1


state(:,:,47) =

    29     1     1     1
    30     1     0     0


state(:,:,48) =

    31     1     0     1
    32     1     1     0


state(:,:,49) =

    33     1     1     0
    34     1     0     1


state(:,:,50) =

    35     1     0     0
    36     1     1     1


state(:,:,51) =

    37     1     0     1
    38     1     1     0


state(:,:,52) =

    39     1     1     1
    40     1     0     0


state(:,:,53) =

    41     1     0     1
    42     1     1     0


state(:,:,54) =

    43     1     1     1
    44     1     0     0


state(:,:,55) =

    45     1     1     0
    46     1     0     1


state(:,:,56) =

    47     1     0     0
    48     1     1     1


state(:,:,57) =

    49     1     1     0
    50     1     0     1


state(:,:,58) =

    51     1     0     0
    52     1     1     1


state(:,:,59) =

    53     1     0     1
    54     1     1     0


state(:,:,60) =

    55     1     1     1
    56     1     0     0


state(:,:,61) =

    57     1     0     1
    58     1     1     0


state(:,:,62) =

    59     1     1     1
    60     1     0     0


state(:,:,63) =

    61     1     1     0
    62     1     0     1


state(:,:,64) =

    63     1     0     0
    64     1     1     1
%%%%%%%%%%%%%%%%%%%

revealed =[1 0];
tmp=[0,0];
for m=1:2
tmp[1]=sum(abs(state(l, 3:4 ,j)-revealed)) % here  state(m, 3:4 ,j) is actually the 3rd and 4th columns substracting from them the array revealed at every row (m=1, 2) of given j.
end
so at the end it returns tmp.

to who doesn't understand matlab is that there's an array state with size 2x4x64 and state(l, 3:4 ,j) gives me the two columns values according to j and then we subtract the array revealed from then andwe do afterwards abs and then sum to the array result.for example: state (m, 3:4 , 1) .J=1 gives me [0 0] and [1 1] for m=1 , m=2 at third and 4th columns. so at every m I subtract revealed[1 0] from the two value of third columns and 4th columns values according to m.so here at m=1 we have [0 0] so [0 0] - revealed => [0 0]-[1 0] => [-1 0] and we do abs([-1 0]) sowe get [1 0] and then we do sum which it's 1+0 => 1.At m=1 we get [1 1] so [1 1]-[1 0] => [1 0] we do abs on this array => [1 0] and then afterwards we do sum 1+0 => 1 .

I'm trying to do the same thing / same concept in java (to convert this code matlab to java) . so what I've done is this:

int[64][2][4] state=
{
{{ 1    , 0 ,    0 ,    0},
 { 2    , 0   ,  1,     1 }},

    {{ 3 ,    0 , 1 ,    0},
    {4   ,  0  ,   0  ,   1}},

    {{  5   ,  0 ,    1  ,   1},
    {6   ,  0 ,    0    , 0}},

    {{ 7    , 0   ,  0    , 1},
    { 8 ,    0  ,   1 ,    0 }}


   { {9     ,0  ,   1   ,  1},
    {10  ,   0   ,  0    , 0}},

    { { 11  ,   0   ,  0 ,    1},
      {12   ,  0   ,  1  ,   0}},




    { { 13  ,   0  ,   0  ,  0},
    {14   ,  0    , 1  ,   1}},




    { { 15  ,   0   ,  1 ,    0},
    { 16  ,   0  ,   0  ,   1} },


    {{17  ,   0  ,   0  ,   0},
    {18  ,   0  ,   1 ,    1 }},

    {{19  ,   0  ,   1   ,  0},
     {20  ,   0  ,   0  ,   1}},

    {{21   ,  0  ,   1 ,    1},
    { 22   ,  0   ,  0 ,    0}},


    { { 23 ,    0 ,    0,     1} ,
    {24  ,   0  ,   1  ,   0}},


    { {25  ,   0  ,   1 ,    1},
    {26   ,  0   ,  0  ,   0}},


    { {27   ,  0  ,   0 ,    1},
      {28  ,   0  ,   1  ,   0}},

    {{29   ,  0  ,   0  ,   0},
     {30  ,   0 ,    1 ,    1}},

    {{ 31   ,  0    , 1  ,   0},
     {32   ,  0  ,  0   ,  1}},


    { { 33 ,    0   ,  0 ,    1},
    {34   ,  0  ,   1 ,    0} },

    {{35  ,   0   ,  1   ,  1},
    {36  ,   0    , 0   ,  0}},

    {{ 37  ,   0  ,   1 ,    0},
     {38   ,  0  ,   0  ,   1}},

    {{39   ,  0  ,   0  ,   0},
     { 40   ,  0  ,   1  ,   1}},


    {{41  ,   0   ,  1 ,    0},
    { 42  ,   0  ,   0  ,   1}},


    {{ 43  ,   0  ,   0 ,    0},
     { 44  ,  0   ,  1  ,   1}}


    {45 ,    0  ,   0  ,   1},
    {46  ,   0  ,   1  ,   0} },


   { { 47   ,  0 ,    1  ,   1 },
     { 48   ,  0  ,   0  ,   0 } },


    {{ 49  ,   0  ,   0  ,   1 },
     { 50  ,   0   ,  1   ,  0 }},




  { { 51   ,  0    , 1 ,    1 },
    {52   ,  0   , 0  ,  0} },



    { { 53  ,   0,     1  ,   0 },
      {54   ,  0 ,   0  ,  1} },



    { { 55  ,   0   ,  0   ,  0 },
      { 56  ,   0   ,  1  ,   1} },


   { {57  ,   0  ,   1  ,   0},
     {58  ,   0  ,   0  ,   1} },



   { { 59   ,  0   ,  0 ,    0 },
      { 60   ,  0   ,  1  ,   1} },



   { { 61 ,    0 ,    0  ,   1 },
     { 62   ,  0  ,   1  ,   0} },



  { { 63   ,  0  ,   1 ,   1},
    { 64  ,  0  ,   0 ,   0 } },
};



 public class GlobalMember
    {
     int[] revealed =[1 0];
     int[] tmp= {0,0};
   for(int j=0 ; j<64 ; j++)  
     { for (int m = 0 ; l < 2 ; l++)
       {      
           tmp[l] = 
             IntStream.of((Math.abs(state[m, 3 : 4, j] - revealed))).sum();
             //note that I always just check 3rd and 4th columns values which 
            //they are implicitly an array with two element at every row of 
           //the two rows at every j(j is range from 0 to 64).
       }
     }
    }

but it sounds Im missing something and it doesn't compile at all, could anyone please help mehow do I implement this code matlab in java? I mean to implement the same concept of matlab code in java.

Im struggling this about one week and I didn't understand where's exactly the problem.

thanks alot.

解决方案

First lets get familiar with the basics. let's start with 2D arrays.

a) 2D Arrays

Imagine 2D arrays not as an grid of numbers but as layers of 1D array's because each of these layers can have an 1D array of any length,lets look at an example

 //This 2D array has 3 layers, each layer is denoted using {} brackets

 int[][] test={
               {1,2}            //Layer 0 has 2 elements
              ,{3,4,5}          //Layer 1 has 3 elements  
              ,{6,7,8,9}        //Layer 2 has 4 elements
             };

To query this code you would do like this

int layers=test.length;  //This gives us the total layers in the 2D array
System.out.println(layers);   
for(int i=0;i<layers;i++){System.out.println(test[i].length);}//This queries the number of elements in each layer 

Output:

3  //Number of layers=3
2  //Layer 0 has 2 elements
3  //Layer 1 has 3 elements
4  //Layer 2 has 4 elements

You can also initialize an 2D array like this

int rows=2;
int cols=3;
int[][] test=new int[rows][cols];

However one limitation is you would have guessed it from above is that you can initialize your 2D array only in an grid fashion. The number of layers is the rows and the length of each 1D array in each layer is the columns.

To iterate over each element you would do it like this

for(int layer=0;layer<test.length;layer++)  //iterate over each layer
{
 for(int i=0;i<test[layer].length;i++)      //for each layer iterate over all elements in that layer
 {
  int element=test[layer][i];
 }
}

b) 3D Arrays

Now just as 2D arrays contain multiple layers of 1D arrays 3D arrays contains multiple layers of 2D arrays, let's look at an example

 int[][][] test=
  {
   {                     //This is layer 0
     {0,1}
    ,{2,3,4}
   }      
   ,     
   {                     //This is layer 1
     {5,6}
    ,{7,8,9}
    ,{10,11,12}
   }     
   ,      
   {                      //This is layer 2
     {13,14}
    ,{15,16,17}
    ,{18,19,20,21}
    ,{22,23,24,25,26}
   }
  };

Again you can also initialize an 3D array like this

int layers=3;
int rows=4;
int col=5;
int[][][] test=new int[layers][rows][col];

With again the limitation that every 2D array layer must be initialized in an grid fashion

In fact you can reference an 2D array from any layer in this 3D array like this

int[][] test2=test[0]; //get the 2D array stored in layer 0 

also to loop over every element in this 3D array you have dig through all the layers

for(int layer2D=0;layer2D<test.length;layer2D++)  //Iterate over every 2D array layer in this 3D array
{
 for(int layer1D=0;layer1D<test[layer2D].length;layer1D++)  //iterate over every 1D layer of this current 2D array
 {
  for(int element=0;element<test[layer2D][layer1D].length;element++) //finally iterate over every element of this current 1D layer or 1D array
  {
   int value=test[layer2D][layer1D][element];
    
  }
 }
}

Ok after all of that let's address your problem now.

1) Fix your array

Since you are initializing your 3D array using approach one you don't need to specify the lengths. Here is your final array

int[][][] state=              //No need to specify lengths just use empty brackets
  {
   {                          //Layer 0
    {1    , 0 ,    0 ,    0},
    {2    , 0   ,  1,     1 }
   }
   ,
   {                          //Layer 1 and so on
    {3 ,    0 , 1 ,    0},
    {4   ,  0  ,   0  ,   1}
   }
   ,
   {
    {5   ,  0 ,    1  ,   1},
    {6   ,  0 ,    0    , 0}
   }
   ,
   {
    {7    , 0   ,  0    , 1},
    {8 ,    0  ,   1 ,    0 }
   }
   ,
   { 
    {9     ,0  ,   1   ,  1},
    {10  ,   0   ,  0    , 0}
   }
   ,
   { 
    {11  ,   0   ,  0 ,    1},
    {12   ,  0   ,  1  ,   0}
   }
   ,
   { 
    {13  ,   0  ,   0  ,  0},
    {14   ,  0    , 1  ,   1}
   }
   ,
   { 
    {15  ,   0   ,  1 ,    0},
    {16  ,   0  ,   0  ,   1} 
   }
   ,
   {
    {17  ,   0  ,   0  ,   0},
    {18  ,   0  ,   1 ,    1 }
   }
   ,
   {
    {19  ,   0  ,   1   ,  0},
    {20  ,   0  ,   0  ,   1}
   }
  ,
  {
   {21   ,  0  ,   1 ,    1},
   {22   ,  0   ,  0 ,    0}
  }
  ,
  { 
   {23 ,    0 ,    0,     1},
   {24  ,   0  ,   1  ,   0}
  }
  ,
  { 
   {25  ,   0  ,   1 ,    1},
   {26   ,  0   ,  0  ,   0}
  }
  ,
  { 
   {27   ,  0  ,   0 ,    1},
   {28  ,   0  ,   1  ,   0}
  }
  ,
  {
   {29   ,  0  ,   0  ,   0},
   {30  ,   0 ,    1 ,    1}
  }
  ,
  {
   {31   ,  0    , 1  ,   0},
   {32   ,  0  ,  0   ,  1}
  }
  ,
  { 
   {33 ,    0   ,  0 ,    1},
   {34   ,  0  ,   1 ,    0} 
  }
  ,
  {
   {35  ,   0   ,  1   ,  1},
   {36  ,   0    , 0   ,  0}
  }
  ,
  {
   {37  ,   0  ,   1 ,    0},
   {38   ,  0  ,   0  ,   1}
  }
  ,
  {
   {39   ,  0  ,   0  ,   0},
   {40   ,  0  ,   1  ,   1}
  }
  ,
  {
   {41  ,   0   ,  1 ,    0},
   {42  ,   0  ,   0  ,   1}
  }
  ,
  {
   {43  ,   0  ,   0 ,    0},
   {44  ,  0   ,  1  ,   1}
  }
  ,
  {
   {45 ,    0  ,   0  ,   1},
   {46  ,   0  ,   1  ,   0} 
  }
  ,
  { 
   {47   ,  0 ,    1  ,   1 },
   {48   ,  0  ,   0  ,   0 } 
  }
  ,
  {
   { 49  ,   0  ,   0  ,   1 },
   { 50  ,   0   ,  1   ,  0 }
  }
  ,
  { 
   {51   ,  0    , 1 ,    1 },
   {52   ,  0   , 0  ,  0} 
  }
  ,
  { 
   {53  ,   0,     1  ,   0 },
   {54   ,  0 ,   0  ,  1} 
  }
  ,
  { 
   {55  ,   0   ,  0   ,  0 },
   {56  ,   0   ,  1  ,   1} 
  }
  ,
  { 
   {57  ,   0  ,   1  ,   0},
   {58  ,   0  ,   0  ,   1} 
  }
  ,
  { 
   {59   ,  0   ,  0 ,    0 },
   {60   ,  0   ,  1  ,   1} 
  }
  ,
  { 
   {61 ,    0 ,    0  ,   1 },
   {62   ,  0  ,   1  ,   0}
  }
  ,
  { 
   {63   ,  0  ,   1 ,   1},
   {64  ,  0  ,   0 ,   0 } 
  }
  };
  1. Fix Your code

This code right here

Won't do you see every program in java begins with a class and an main method and so everything you do needs to encapsulated in an method which is further inside an class you can read this simple tutorial

Let's put that in an method and forgive me if i don't understand your logic but basically if you are trying to find the sum of all elements you have to declare an variable to store the result and keep adding to it to get your result. While we are at it let's declare your main method as well in this class

public class FindSum
{
 public static void main(String[] args)
 {
  int[][][] state=//The array i showed above cause i can't be asked to paste that here again

  System.out.println(findSum(state)); //You call that function here
 }

 public static int findSum(int[][][] state)
 {
  int sum=0;

  //Using the same logic from above

   for(int layer2D=0;layer2D<state.length;layer2D++)  //Iterate over every 2D array layer in this 3D array
   {
     for(int layer1D=0;layer1D<state[layer2D].length;layer1D++)  //iterate over every 1D layer of this current 2D array
     {
      for(int element=0;element<state[layer2D][layer1D].length;element++) //finally iterate over every element of this current 1D layer or 1D array
      {
       int value=state[layer2D][layer1D][element];
        
       sum+=value;
      }
     }
   }
   return sum;
 }
}

I have covered only the basics here So find some good tutorials and there a lot out there.

这篇关于Java(内置方法)的实现中的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 01:59