-3

i have beginner question in java, as i don't know what is wrong with my array, because i just can't index through them. yes i know there is another faster way to check palindrome but pls take a look.

public boolean palindrom (String a){
    List<String> normal = new ArrayList<String>();
    List<String> modified = new ArrayList<String>();
    for (String x: a.split("")){
        normal.add(x);
    }

    for (String x:new StringBuilder(a).reverse().toString().split("")){
        modified.add(x);
    }

    for (int i=0;i<a.split("").length;i++){
        if (normal[i]!=modified[i]){
          //in this line above is error as it doesnt recognise "normal" and "modified" arrays
            return false;
        }
    }
    return true;
Jernej Habjan
  • 148
  • 1
  • 14
  • 1
    What do you mean by *can't index through them*? What is the issue exactly? – Atri Feb 14 '16 at 18:53
  • `normal` and `modified` are `List` and not `Array`, you cannot reference like `normal[i]`. You need to do `normal.get(i)`. – Atri Feb 14 '16 at 18:54
  • the error i get is The type of the expression must be an array type but it resolved to List. i believe array is not defined right – Jernej Habjan Feb 14 '16 at 18:55
  • You should add error information to your question so others wouldn't need to search for them in comments. Use [edit] option for that. – Pshemo Feb 14 '16 at 18:55
  • 1
    Also to prevent your next question read: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Feb 14 '16 at 18:55

2 Answers2

1

Those are not Arrays but ArrayList
To get element i you have to do normal.get(i)

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Flikk
  • 520
  • 3
  • 10
0

Lists aren't indexed in the same way arrays are. Rather than use normal[i], you would use normal.get(i).

for (int i=0;i<a.split("").length;i++){
    if (normal.get(i) != modified.get(i)){
        return false;
    }
}
lhoworko
  • 1,191
  • 2
  • 13
  • 24