int

Splitting a string separated by commas and storing the value into an int array

萝らか妹 提交于 2020-01-30 10:59:06
问题 I have a string which I want to split by the commas (easy process): String numbers = "1,2,3"; But, I then want to convert each element of the split string into an int that can be stored in an int array separated by commas as: int[][] array = new int [0][]; array[0] = new int[] {1,2,3}; How can I achieve this? 回答1: The 2D array is unnecessary. All you need to do is splice the commas out of the string using the split method. Then convert each element in the resulting array into an int. Here's

How can I find if my ArrayList of int[] contains an int[]

妖精的绣舞 提交于 2020-01-30 09:09:06
问题 I have an ArrayList containing int[] . Assuming Arrays.equals(int[], (a value in the arrayList)) = true why when I do arrayOfInts.contains(int[]) is this false? I assume it is because of the same reason that (int[] == (a value in the arrayList)) = false but should I be getting this error? Can I correct it somehow? TL;DR: Can I use Arraylist.contains() for int[] EDIT: ANSWER (if anyone searches this in the future here's the comparison code I made as a solution): private boolean

for loop terminating early when comparing to Integer.MAX_VALUE and using System.out.println

余生颓废 提交于 2020-01-29 12:04:48
问题 When I run this class the for loop seems to terminate early class Test { public static void main(String[] args) { int result = 0; int end = Integer.MAX_VALUE; int i; for (i = 1; i <= end; i += 2) { System.out.println(i); } System.out.println("End:" + i); } } Output is: 1 3 5 ... 31173 31175 End:31177 Why does it end there? Interestingly if I removed the System.out.println(i) in the for loop, the output would be End:-2147483647 . Obviously the value in i has wrapped round . The Java version I

JOptionPane Input to int

戏子无情 提交于 2020-01-29 04:55:07
问题 I am trying to make a JOptionPane get an input and assign it to an int but I am getting some problems with the variable types. I am trying something like this: Int ans = (Integer) JOptionPane.showInputDialog(frame, "Text", JOptionPane.INFORMATION_MESSAGE, null, null, "[sample text to help input]"); But I am getting: Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer Which sounds logical yet, I cannot think of another way to make this

Concatenate Swift Array of Int to create a new Int

我只是一个虾纸丫 提交于 2020-01-28 07:47:09
问题 How can you make an Array<Int> ( [1,2,3,4] ) into a regular Int ( 1234 )? I can get it to go the other way (splitting up an Int into individual digits), but I can't figure out how to combine the array so that the numbers make up the digits of a new number. 回答1: This will work: let digits = [1,2,3,4] let intValue = digits.reduce(0, combine: {$0*10 + $1}) For Swift 4+ : let digits = [1,2,3,4] let intValue = digits.reduce(0, {$0*10 + $1}) Or this compiles in more versions of Swift: (Thanks to

What exactly does Double mean in java?

佐手、 提交于 2020-01-28 04:29:30
问题 I'm extremely new to Java and just wanted to confirm what Double is? Is it similar to Float or Int? Any help would be appreciated. I also sometimes see the uppercase Double and other times the lower case double . If someone could clarify what this means that'd be great! 回答1: Double is a wrapper class, The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods

What exactly does Double mean in java?

笑着哭i 提交于 2020-01-28 04:28:48
问题 I'm extremely new to Java and just wanted to confirm what Double is? Is it similar to Float or Int? Any help would be appreciated. I also sometimes see the uppercase Double and other times the lower case double . If someone could clarify what this means that'd be great! 回答1: Double is a wrapper class, The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods

int[] and Integer[] arrays - What is the difference?

早过忘川 提交于 2020-01-27 04:13:24
问题 Considering following basics: Any Object lives only on heap, Array IS-A Object and Integer IS-A Object I find myself difficult to answer such a simple question: Is there any difference between int[] and Integer[] inside of JVM ? Or it makes sense only at "compile-time"? 回答1: There is a difference at run-time. int[] is an array of primitive int values. Integer[] is an "object" array, holding references to Integer objects. Most important practical difference: int[] cannot hold null values. But

Or operand with int in if statement

那年仲夏 提交于 2020-01-25 12:53:17
问题 My problem is that program is not reading codes as i intended "he" would. I have if (hero.getPos() == (6 | 11 | 16)) { move = new Object[] {"Up", "Right", "Left"}; } else { move = new Object[] {"Up", "Down", "Right", "Left"}; } When hero position is 6, the program still goes to else. Why is that? Is it because of operands? If yes, how should i change it? 回答1: Use: if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) { This will do what you want. What you did is comparing

TypeError: 'str' object cannot be interpreted as an integer even though I assigned it with int(value)

有些话、适合烂在心里 提交于 2020-01-25 08:17:09
问题 When I type in my simple code: times = input("How many times do I have to tell you? ") times = int(times) for i in range(times): print("Clean your room!") I get the following error message: >>> times = input("How many times do I have to tell you? ") How many times do I have to tell you? times = int(times) >>> >>> for time in range(times): ... print("Clean your room!") ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object cannot be interpreted as an