+ 2
[SOLVED] When I loop an Array, and I want to make an if else statement with the array.next() block. What can I do?
It doesn't work because it's not a string and tbe if else statement says 'false', how can I fix it? https://code.sololearn.com/c9UOuV3KiZjJ/?ref=app Thanks
7 Answers
+ 2
ArrayList corr = new ArrayList();
When you create an ArrayList like this, then all values will be from type Object.
Array<Character> or ArrayList<String> list = new ArrayList<>()
Now the list knows the type.
== works only for primive data types (int, char, double...)
But String is an object. If you compare objects you have to use equals.
if(myString.equals(anotherString)){
And why are you using try / catch?
+ 2
No. I am not sure what.you want to do:
a) convert int to string
b) try a hashmap
c) use two arrays
+ 1
I'm using try catch because if the index is bigger than the length of the list, it throws an error. Anyway thanks for the information
+ 1
Orango Mango
I understand it. ;)
But instead of catching such exceptions you should avoid them.
You know the length and you know that the index starts at 0.
Instead of an iterator you can use a normal loop:
for(int i = 0; i < current.length - 2; i++){
i cannot be larger than current.length - 2 so you will not get a problem if you search for an index i + 2
+ 1
No. This won't work.
0
Thanks, but can I make an Array with more Objects type like <String><Int> ?
0
So I can't make an Array containing Ints and Strings?