I'm trying to create a program that checks if a word is a palindrome. Is it possible to actively add to an array during runtime? I want to pull characters from a string into an array of chars to compare them, but I don't know how large the array will have to be.
Asked
Active
Viewed 39 times
0
-
1"Is it possible to actively add to an array during runtime?" No. Look into using an `ArrayList` instead, which is resizable. – resueman Jun 21 '15 at 00:41
-
@resueman can you post that as an answer so I can mark this questions answered? Thanks. – Wesley Luh Jun 21 '15 at 00:43
-
1Your question is imprecise. But I think you are asking if it is possible to do an operation that causes the length of an array to change. The answer to that is No. (And the question has been asked before .... many times.) – Stephen C Jun 21 '15 at 00:43
-
If you're extracting characters from a String, a [`StringBuilder`](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) (a mutable sequence of characters) might be a better fit than a `List`. – Mick Mnemonic Jun 21 '15 at 00:44
-
Actually, if he is extracting characters from an String, then `toCharArray()` is simpler. – Stephen C Jun 21 '15 at 00:48
-
@StephenC, it of course depends on what the OP is actually doing. `toCharArray()` returns an array which is not "simple" to manipulate. – Mick Mnemonic Jun 21 '15 at 00:59
-
I guess what you're looking for is char[] myCharArray = str.toCharArray(); – Raman Shrivastava Jun 21 '15 at 01:13
-
@MickMnemonic - Last time I checked, a palindrome is the same length in both directions :-) – Stephen C Jun 21 '15 at 02:01
-
@StephenC Thanks. I used that and it, well, half-worked. – Wesley Luh Jun 22 '15 at 00:27