2016-10-27 32 views
1

我想建立一個二維數組,可容納四個數學運算符的每3項組合的各種可能組合(+ - * /) 數組應該是這個樣子
[ - , - , - ]
[ - ,*,*]
[ - ,/,/]

......
[/,*,*]
[/,/, /]
但是我遇到了一些問題。我的代碼在打印組合時,每個組合打印3次。其次,我不太確定如何讓每個操作員進入陣列中的位置。
這裏是我的代碼
填充二維數組與3個數學運算符

String[][] allOps = new String[27][3]; 
    int count = 1; 

    String[] ops = new String[] {"+","-","*","/"}; 
    for (int x=1;x<4;x++) 
    { 
     for (int y=1;y<4;y++) 
     { 
      for (int z=1;z<4;z++) 
      { 


       //System.out.println(ops[x] + " " + ops[y] + " " + ops[z] + " " + count); 
       //count++; 
      } 
     } 
    } 

有沒有什麼辦法讓每個運營商在它的具體位置([*] [*] [*])使用一個for循環?

+0

數組是0索引。您在'for'循環中的索引應該從0開始。 – 4castle

+1

您有一個錯字。第三個值應該使用'ops [z]'而不是'ops [y]'。 [它工作正常](https://ideone.com/L2p0GE)。 – 4castle

+0

@ 4castle但是,如果你仔細觀察你的輸出,你會發現有重複的,例如' - - +'和'+ - -'和' - + -' ...不確定OP是否在意這一點,但只是想指出這一點。 –

回答

0

試試這個,你需要構建新的數組與運營商,並在索引中添加它從零開始,你也將得到最大的組合是64

String[][] allOps = new String[64][3]; 
    int count = 0; 
    for (int x = 0; x < 4; x++) { 
     for (int y = 0; y < 4; y++) { 
      for (int z = 0; z < 4; z++) { 
       allOps[count][0] = ops[x]; 
       allOps[count][1] = ops[y]; 
       allOps[count][2] = ops[z]; 
       count++; 
      } 
     } 
    } 
    System.out.println(count); 
    System.out.println(Arrays.deepToString(allOps)); 

隨着java8流

String[] ops = new String[] { "+", "-", "*", "/" }; 
    List<String> opsList = Arrays.asList(ops); 
    String[][] combinations = opsList.stream() 
           .flatMap(x -> opsList.stream().flatMap(y -> opsList.stream().map(z -> new String[] { x, y, z }))) 
           .toArray(String[][]::new); 
    System.out.println(Arrays.deepToString(combinations)); 

組合

[[+, +, +], [+, +, -], [+, +, *], [+, +, /], [+, -, +], [+, -, -], [+, -, *], [+, -, /], [+, *, +], [+, *, -], [+, *, *], [+, *, /], [+, /, +], [+, /, -], [+, /, *], [+, /, /], [-, +, +], [-, +, -], [-, +, *], [-, +, /], [-, -, +], [-, -, -], [-, -, *], [-, -, /], [-, *, +], [-, *, -], [-, *, *], [-, *, /], [-, /, +], [-, /, -], [-, /, *], [-, /, /], [*, +, +], [*, +, -], [*, +, *], [*, +, /], [*, -, +], [*, -, -], [*, -, *], [*, -, /], [*, *, +], [*, *, -], [*, *, *], [*, *, /], [*, /, +], [*, /, -], [*, /, *], [*, /, /], [/, +, +], [/, +, -], [/, +, *], [/, +, /], [/, -, +], [/, -, -], [/, -, *], [/, -, /], [/, *, +], [/, *, -], [/, *, *], [/, *, /], [/, /, +], [/, /, -], [/, /, *], [/, /, /]] 
0

您有一個小錯誤,將您的最後一個操作符[y]更改爲ops [z]並將ur x = 1更改爲x = 0,y = 1至y = 0,z = 1至z = 0,是因爲陣列從0-3開始而不是1-3,現在你可以看到帶有「+」的組合,如果你覺得有幫助,標記爲回答。

String[][] allOps = new String[27][3]; 
int count = 1; 

String[] ops = new String[] {"+","-","*","/"}; 
for (int x=0;x<4;x++) 
{ 
    for (int y=0;y<4;y++) 
    { 
     for (int z=0;z<4;z++) 
     { 


      System.out.println(ops[x] + " " + ops[y] + " " + ops[z] + " " + count); 
      count++; 
     } 
    } 
} 
+0

謝謝!我想我從來沒有注意到這個錯誤。但是,你能幫我解決第二個問題嗎?我怎樣才能把每個操作符放入二維數組中? – Sagar

+0

String [] ops = new String [] {「+」,「 - 」,「*」,「/」};這已經是一個2D數組 – bingcheng45

0

有64個(4 * 4 * 4)四個運算符*, - ,/和+排列成3個位置的組合。你需要在這裏稍微改變你的代碼,你的for循環從索引0開始,然後繼續循環。這裏怎麼了,雖然不知道你爲什麼要二維數組

String[][] arrayOfCombination = new String[64][1]; 
    int count = 0; 
    String[] ops = new String[] { "+", "-", "*", "/" }; 
    for (int x = 0; x < 4; x++) { 
     for (int y = 0; y < 4; y++) { 
      for (int z = 0; z < 4; z++) { 

       arrayOfCombination[count][0] = ops[x] + " " + ops[y] + " " + ops[z]; 
       count++; 
      } 
     } 
    } 
    for (int row = 0; row < 64; row++) { 

     System.out.println(arrayOfCombination[row][0]); 

    } 

更好的辦法是使用ArrayList如下,

ArrayList<String> list = new ArrayList(); 

//adding combinations 
list.add("[" + ops[x] + " " + ops[y] + " " + ops[z] + "]"); 

//printing them 
System.out.println(list);