Search code examples
javaarrayspalindrome

Java palindrome


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;

Solution

  • 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