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

问题描述

大家好,新年快乐.

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

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.

谁不懂matlab的

是,有一个大小为2x4x64的数组状态,并且state(l,3:4,j)给我两个根据j的列值,然后我们减去从那时开始显示的数组,然后我们先做abs,然后求和到数组结果.例如:state(m,3:4,1).J = 1给我[0 0]和[1 1]表示m = 1,在第三和第四列时m = 2.所以我减去的每m从第三列的第二个值和第四列的两个值中取[1 0]对应于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],然后我们求和1 + 0 =>1.

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 .

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

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).
       }
     }
    }

但听起来我缺少某些东西,而且根本无法编译,有人可以帮我吗如何在Java中实现此代码matlab?我的意思是要在Java中实现相同的Matlab代码概念.

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.

非常感谢.

推荐答案

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

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

a)2D阵列

想象一下2D数组不是数字网格而是1D数组的层,因为这些层中的每一层都可以具有任意长度的1D数组,请看一个例​​子

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 

输出:

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

您也可以像这样初始化2D数组

You can also initialize an 2D array like this

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

但是,从上面您已经猜到了一个限制,那就是只能以网格方式初始化2D数组.层数是行,每层中每个1D数组的长度是列.

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阵列

现在,就像2D数组包含多层1D数组一样3D数组包含多层2D数组,让我们看一个例子

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

同样,您也可以像这样初始化3D数组

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

再次有一个限制,即必须以网格方式初始化每个2D数组层

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

实际上,您可以像这样从3D数组中的任何层引用2D数组

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 

还可以遍历您已遍历所有图层的3D阵列中的每个元素

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)修复数组

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

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. 修正您的代码

此代码在这里

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).
       }
     }
    }

您不会看到Java中的每个程序都以一个类和一个main方法开头,因此您需要做的所有事情都需要封装在一个类中的方法中,您可以阅读以下简单的教程

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 02:01