如何在Int数组中存储String?

时间:2022-05-28 01:28:55

I have a question. My teacher has asked as part of my assignment, to replace all 0's (zero's) with asterisk's (*'s) in my array.

我有个问题。我的老师在我的任务中要求用我的数组中的星号(*)代替所有0(零)。

This is an example of the output of my program:

这是我的程序输出的一个例子:

Best Tour: #31
Number of Moves: 57
=====================================
 1   16   57    6    0   36   47    0   
56    7    0    3   46    5   32   37   
17    2   15    8   33   30   35   48   
14   55   22   45    4   49   38   31   
23   18   13   50    9   34   29   42   
54   51   24   21   44   41   26   39   
0    12   19   52   25   10   43   28   
0    53    0   11   20   27   40    0   
=====================================

Basically, everywhere there is a zero, he wants it to instead show an asterisk. This is an int array. I have tried creating a String with the value " * ". Then I tried parsing it into an int, but I get:

基本上,到处都是零,他希望它显示一个星号。这是一个int数组。我尝试使用值“*”创建一个String。然后我尝试将其解析为int,但我得到:

Exception in thread "main" java.lang.NumberFormatException: For input string: "*"

Any other suggestions I should try out?

我应该尝试其他任何建议吗?


Thank you guys so much! I was just not looking at things correctly. After reading some comments, I realized that it didn't make much sense to try to change the zero's to asterisk's in the array itself.

非常感谢你们!我只是没有正确地看待事情。在阅读了一些评论之后,我意识到尝试将零点更改为数组本身中的星号是没有多大意义的。

So I modified my printArray() method, so that in any instance of 0, it will print *; else it just prints whatever number is supposed to be there.

所以我修改了我的printArray()方法,这样在任何0的实例中,它都会打印*;否则它只会打印应该在那里的任何数字。

Here is what it looks like now:

这是现在的样子:

Best Tour: #45
Number of Moves: 58
=====================================
 1   42   13   18   31   44   27    *   
58   11   32   43   14   17   30   45   
41    2   57   12   19   26   15   28   
56   33   10    5   16   29   46    *   
 3   40   55   34    9   20   25   50   
 *   37    4   53    6   49   22   47   
39   54   35    8   21   24   51    *   
36    *   38    *   52    7   48   23   
=====================================

You all are so helpful, and very quick I might add! Thank you so much!

你们都非常乐于助人,我可以加快!非常感谢!

5 个解决方案

#1


3  

You can't reliably store much of anything but ints in an array of ints, if you care about the values of the ints too. (You could convert the char * to an int if you really really wanted to, but the ASCII code for it (42) would put it right there with potentially valid values for the ints, and cause you all kinds of grief later.)

如果你也关心整数的值,你不能可靠地存储大量的整数而不是整数的整数。 (你可以把char *转换成一个int,如果你真的真的想要,但是它的ASCII代码(42)会把它放在那里,有一些潜在有效的int值,并且会引起各种各样的悲伤。)

So you have two realistic choices:

所以你有两个现实的选择:

  • Store strings instead of ints, convert everything to strings, and store "*" instead of "0"; or
  • 存储字符串而不是整数,将所有内容转换为字符串,并存储“*”而不是“0”;要么

  • Only worry about the asterisks as you output the array, and wherever the value is 0, just output "*" instead.
  • 只需在输出数组时担心星号,只要值为0,只需输出“*”即可。

The latter is far simpler, and leaves your ints as ints so you can still easily do math with them later if you want.

后者更简单,并且将你的整数保留为整数,这样如果你愿意,你仍然可以轻松地用它们进行数学运算。

#2


2  

Instead of trying to put the asterisk inside the int array, focus on when you're outputting the area to the screen. Create a method to display the array like you have it formatted in the question, and when you're printing the numbers, be sure to check their values carefully.

而不是试图将星号放在int数组中,而是在将区域输出到屏幕时关注。创建一个方法来显示数组,就像你在问题中格式化它一样,当你打印数字时,一定要仔细检查它们的值。

Note: I'm not giving away the answer because this question is .

注意:我没有给出答案,因为这个问题是作业。

#3


0  

You should deal with the *s when you are printing, not replace the 0s with them in the array itself. Something like this (analogous method can be used in 2D-case):

你应该在打印时处理* s,而不是在数组本身中用它们替换0。像这样的东西(类似的方法可以在2D情况下使用):

int[] a = new int[]{1, 2, 0, 5, 9, 3, 0};
for (int i : a)
    // If ("a" is 0) then print "*", otherwise print "a"

#4


0  

FIRST: you cannot add String into int[] array. so, you could do something like below. as this a homework i dint write the whole code.

FIRST:你不能将String添加到int []数组中。所以,你可以做类似下面的事情。因为这是一个功课,我写了整个代码。

int[] x= {0,2,4,0,7};
String[] str= new String[x.length];
for(loop thru int array) {
    // check if the indexed element is equal to 0
    // if it is equal to 0 then add '*' into str
    // else add the number at the index into str
}

if you want to print them loop thru str[] and print each element

如果你想通过str []循环打印它们并打印每个元素

#5


-5  

i see there's 8 array, make sure to validate the array one by one if array contains zero, replace zero with *

我看到有8个数组,如果数组包含零,请确保逐个验证数组,用*替换为零

#1


3  

You can't reliably store much of anything but ints in an array of ints, if you care about the values of the ints too. (You could convert the char * to an int if you really really wanted to, but the ASCII code for it (42) would put it right there with potentially valid values for the ints, and cause you all kinds of grief later.)

如果你也关心整数的值,你不能可靠地存储大量的整数而不是整数的整数。 (你可以把char *转换成一个int,如果你真的真的想要,但是它的ASCII代码(42)会把它放在那里,有一些潜在有效的int值,并且会引起各种各样的悲伤。)

So you have two realistic choices:

所以你有两个现实的选择:

  • Store strings instead of ints, convert everything to strings, and store "*" instead of "0"; or
  • 存储字符串而不是整数,将所有内容转换为字符串,并存储“*”而不是“0”;要么

  • Only worry about the asterisks as you output the array, and wherever the value is 0, just output "*" instead.
  • 只需在输出数组时担心星号,只要值为0,只需输出“*”即可。

The latter is far simpler, and leaves your ints as ints so you can still easily do math with them later if you want.

后者更简单,并且将你的整数保留为整数,这样如果你愿意,你仍然可以轻松地用它们进行数学运算。

#2


2  

Instead of trying to put the asterisk inside the int array, focus on when you're outputting the area to the screen. Create a method to display the array like you have it formatted in the question, and when you're printing the numbers, be sure to check their values carefully.

而不是试图将星号放在int数组中,而是在将区域输出到屏幕时关注。创建一个方法来显示数组,就像你在问题中格式化它一样,当你打印数字时,一定要仔细检查它们的值。

Note: I'm not giving away the answer because this question is .

注意:我没有给出答案,因为这个问题是作业。

#3


0  

You should deal with the *s when you are printing, not replace the 0s with them in the array itself. Something like this (analogous method can be used in 2D-case):

你应该在打印时处理* s,而不是在数组本身中用它们替换0。像这样的东西(类似的方法可以在2D情况下使用):

int[] a = new int[]{1, 2, 0, 5, 9, 3, 0};
for (int i : a)
    // If ("a" is 0) then print "*", otherwise print "a"

#4


0  

FIRST: you cannot add String into int[] array. so, you could do something like below. as this a homework i dint write the whole code.

FIRST:你不能将String添加到int []数组中。所以,你可以做类似下面的事情。因为这是一个功课,我写了整个代码。

int[] x= {0,2,4,0,7};
String[] str= new String[x.length];
for(loop thru int array) {
    // check if the indexed element is equal to 0
    // if it is equal to 0 then add '*' into str
    // else add the number at the index into str
}

if you want to print them loop thru str[] and print each element

如果你想通过str []循环打印它们并打印每个元素

#5


-5  

i see there's 8 array, make sure to validate the array one by one if array contains zero, replace zero with *

我看到有8个数组,如果数组包含零,请确保逐个验证数组,用*替换为零