从字符串2d数组中获取元素

时间:2011-05-12 22:35:41

标签: java arrays multidimensional-array

我有一个简单的问题。如何从这个2d数组中获取一个元素:

String s2box[][]= {{"f","3","0","d"},{"1","d","e","8"},{"8","4","7","a"}, {"e","7","b","1"},{"6","f","a","3"},{"b","2","4","f"},{"3","8","d","4"},{"4","e","1","2"},{"9","c","5","b"},{"7","0","8","6"},{"2","1","c","7"},{"d","a","6","c"},{"c","6","9","0"},{"0","9","3","5"},{"5","b","2","e"},{"a","5","f","9"}};

我是这样想的, String sboxl = s2box[2,3];

我想以此元素为例,而不是将其转换为二进制数。

为什么这不起作用?

2 个答案:

答案 0 :(得分:2)

你应该试试这个:

String sbox1 = s2box[2][3];

您使用的语法对于Java不正确。另一种看待它的方式是:

String[] sboxTemp = s2box[2];
String sbox1 = sboxTemp[3];

答案 1 :(得分:2)

要从数组中获取元素并将其转换为相应的二进制模式,您需要执行以下操作:

String s2box[][]= {{"f","3","0","d"},{"1","d","e","8"},{"8","4","7","a"}, {"e","7","b","1"},{"6","f","a","3"},{"b","2","4","f"},{"3","8","d","4"},{"4","e","1","2"},{"9","c","5","b"},{"7","0","8","6"},{"2","1","c","7"},{"d","a","6","c"},{"c","6","9","0"},{"0","9","3","5"},{"5","b","2","e"},{"a","5","f","9"}};
String sbox1 = s2box[2][3];
String sbox1Binary = Integer.toBinaryString(Integer.valueOf(sbox1, 16).intValue());
System.out.println(sbox1Binary);

<强>输出: 1010