Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有字符数组dis函数的Java for循环_Java_Arrays_For Loop_Multidimensional Array - Fatal编程技术网

带有字符数组dis函数的Java for循环

带有字符数组dis函数的Java for循环,java,arrays,for-loop,multidimensional-array,Java,Arrays,For Loop,Multidimensional Array,我在这里遇到了一个循环问题,我正在编写一个脚本,它将接收字符串“geij”或“abab”,并且必须将其转换为双精度,如“6478”或“0101”。借助二维数组,我实现了从字母到数字的转换: String crypt = "geij"; char twoD[][] = {{'a','b','c','d','e','f','g','h','i','j'}, {'0','1','2','3','4','5','6','7','8','9'}}; 首先,我将字符串传递到char数组中: char

我在这里遇到了一个循环问题,我正在编写一个脚本,它将接收字符串“geij”或“abab”,并且必须将其转换为双精度,如“6478”或“0101”。借助二维数组,我实现了从字母到数字的转换:

String crypt = "geij"; 

char twoD[][] = {{'a','b','c','d','e','f','g','h','i','j'}, {'0','1','2','3','4','5','6','7','8','9'}};
首先,我将字符串传递到char数组中:

char tab[] = crypt.toCharArray();
然后我使用循环将字母转换为数字:

for(int c=0;c<tab.length;c++) {
    for(int z=0;z<twoD.length;z++) {
        if(tab[c] == twoD[0][z]) {          
            tab[c] = twoD[1][z];
    }
}
我把这根绳子变成了一根双绞线

double finalC = Double.parseDouble(second);
这个循环的问题是,如果字符串crypt是“abab”,循环将返回0101,正如它应该返回的那样,但是如果字符串包含二维数组的第一个数组中“a”或“b”之后的任何字母,例如字符串“geij”,程序将只返回“geij”。 我不明白为什么这个程序没有超过b,它开始给我一个书呆子。如果有人有主意,我将不胜感激

下面是字符串“abcd”循环后选项卡数组内部的示例:


问题出在内部循环中:
twoD.length
为2,因为
twoD
包含两个内部字符数组

您应该使用
twoD[0].length

for(int c=0; c<tab.length; c++) {
  for(int z=0; z<twoD[0].length; z++) {
    ...

for(int c=0;c问题在于内部循环:
twoD.length
为2,因为
twoD
包含两个内部字符数组

您应该使用
twoD[0].length

for(int c=0; c<tab.length; c++) {
  for(int z=0; z<twoD[0].length; z++) {
    ...

for(int c=0;ctwoD数组的长度为2。第二个循环应该从
z=0
迭代到
twoD[0]。长度


尝试有意义地命名变量,以便更容易发现此类错误。还可以查看foreach循环,这样就不必担心索引。Java映射可以更好地将字符映射到数字。

twoD数组的长度为2。第二个循环应该从
z=0
迭代到
twoD[0]。长度

尝试有意义地命名变量,以便更容易发现这样的错误。还可以查看foreach循环,这样就不必担心索引问题。Java映射可以更好地将字符映射到数字。

Kevin Cruijssen解决了您的问题,但您可以做更多:

使用HashMap解决此问题。现在,您的算法时间复杂度是O(n*m)(n-基字符串长度,m-表中的字母数),因为您必须为每个字母迭代整个字母数组

使用HashMap,您可以在O(1)中找到正确的字母。速度要快得多。因此,现在您的算法具有O(n)时间复杂度

简单的例子:

Map<Character, Integer> encoding = new HashMap<>();
encoding.put('a', 0);
encoding.put('b', 1);
encoding.put('c', 2);
encoding.put('d', 3);

String toEncode = "abcd";
char[] chars = toEncode.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
    int newInt = encoding.getOrDefault(c, -5); //-5 is just a flag that there is no char to encode
    if(newInt == -5){
       continue; //or do something else, e.g throw exception;
    }
    sb.append(newInt);
}

System.out.println(sb.toString());
//Parse double if you want, but remember that what *Nikolas* said in the comments under your post.
//Double.parseDouble(sb.toString());
Map encoding=newhashmap();
编码.put('a',0);
编码。put('b',1);
编码。put('c',2);
编码。put('d',3);
字符串toEncode=“abcd”;
char[]chars=toEncode.toCharArray();
StringBuilder sb=新的StringBuilder();
for(char c:chars){
int newInt=encoding.getOrDefault(c,-5);/-5只是一个没有要编码的字符的标志
如果(newInt==-5){
继续;//或执行其他操作,例如抛出异常;
}
某人追加(newInt);
}
System.out.println(sb.toString());
//如果你想的话,可以用double进行解析,但请记住*Nikolas*在你的帖子下面的评论中所说的话。
//Double.parseDouble(sb.toString());
凯文·克鲁伊森解决了您的问题,但您可以做更多:

使用HashMap解决此问题。现在,您的算法时间复杂度是O(n*m)(n-基字符串长度,m-表中的字母数),因为您必须为每个字母迭代整个字母数组

使用HashMap,您可以在O(1)中找到正确的字母。速度要快得多。因此,现在您的算法具有O(n)时间复杂度

简单的例子:

Map<Character, Integer> encoding = new HashMap<>();
encoding.put('a', 0);
encoding.put('b', 1);
encoding.put('c', 2);
encoding.put('d', 3);

String toEncode = "abcd";
char[] chars = toEncode.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
    int newInt = encoding.getOrDefault(c, -5); //-5 is just a flag that there is no char to encode
    if(newInt == -5){
       continue; //or do something else, e.g throw exception;
    }
    sb.append(newInt);
}

System.out.println(sb.toString());
//Parse double if you want, but remember that what *Nikolas* said in the comments under your post.
//Double.parseDouble(sb.toString());
Map encoding=newhashmap();
编码.put('a',0);
编码。put('b',1);
编码。put('c',2);
编码。put('d',3);
字符串toEncode=“abcd”;
char[]chars=toEncode.toCharArray();
StringBuilder sb=新的StringBuilder();
for(char c:chars){
int newInt=encoding.getOrDefault(c,-5);/-5只是一个没有要编码的字符的标志
如果(newInt==-5){
继续;//或执行其他操作,例如抛出异常;
}
某人追加(newInt);
}
System.out.println(sb.toString());
//如果你想的话,可以用double进行解析,但请记住*Nikolas*在你的帖子下面的评论中所说的话。
//Double.parseDouble(sb.toString());

因为在您的例子中,字符似乎随着其
int
值而递增,所以您根本不需要映射。您可以将字符强制转换为int,然后减去
a
的int值。这是的回答的一个微小变化:

String toEncode = "abcd";
char[] chars = toEncode.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
    int newInt = c - 'a';
    if (newInt < 0 || newInt > ('j'-'a')) {
        continue; //or do something else, e.g throw exception;
    }
    sb.append(newInt);
}

System.out.println(sb.toString());
String-toEncode=“abcd”;
char[]chars=toEncode.toCharArray();
StringBuilder sb=新的StringBuilder();
for(char c:chars){
int newInt=c-‘a’;
if(newInt<0 | | newInt>('j'-'a')){
继续;//或执行其他操作,例如抛出异常;
}
某人追加(newInt);
}
System.out.println(sb.toString());

因为在您的例子中,字符似乎随着其
int
值而递增,所以您根本不需要映射。您可以将字符强制转换为int,然后减去
a
的int值。这是的回答的一个微小变化:

String toEncode = "abcd";
char[] chars = toEncode.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
    int newInt = c - 'a';
    if (newInt < 0 || newInt > ('j'-'a')) {
        continue; //or do something else, e.g throw exception;
    }
    sb.append(newInt);
}

System.out.println(sb.toString());
String-toEncode=“abcd”;
char[]chars=toEncode.toCharArray();
StringBuilder sb=新的StringBuilder();
for(char c:chars){
int newInt=c-‘a’;
if(newInt<0 | | newInt>('j'-'a')){
继续;//或执行其他操作,例如抛出异常;
}
某人追加(newInt);
}
System.out.println(sb.toString());

A String
0101
与double
0101
不同。我知道,但问题不在于转换为double,而是循环没有超过0。对于“b”,请阅读“如何创建A”。然后使用链接改进您的问题(不要通过评论添加更多信息).否则我们无法回答您的问题并帮助您。twoD.长度等于