+ 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

30th Apr 2020, 8:57 AM
Orango Mango
Orango Mango - avatar
7 Antworten
+ 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?
30th Apr 2020, 11:32 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
No. I am not sure what.you want to do: a) convert int to string b) try a hashmap c) use two arrays
30th Apr 2020, 7:10 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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
30th Apr 2020, 11:49 AM
Orango Mango
Orango Mango - avatar
+ 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
30th Apr 2020, 11:58 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
No. This won't work.
30th Apr 2020, 12:56 PM
Denise Roßberg
Denise Roßberg - avatar
0
Thanks, but can I make an Array with more Objects type like <String><Int> ?
30th Apr 2020, 12:54 PM
Orango Mango
Orango Mango - avatar
0
So I can't make an Array containing Ints and Strings?
30th Apr 2020, 1:03 PM
Orango Mango
Orango Mango - avatar