user472221
user472221

Reputation: 3114

this code doesn't print the correct index!

Hi This is my complete code ! I have two lists in it ,one is in increasing order (inorder list) and the other is not!it must print 0 but it will print -1 ,please help me thanks!

public class NewClass{

 private static List<Element> list;
 private static List<Element> inorder;

public NewClass(List listOne, List inorderOne) {
    list = new ArrayList(listOne);
    inorder = new ArrayList(inorderOne);
    FindAllowedTrees((ArrayList<Element>) list);

}
public static void FindAllowedTrees(ArrayList<Element> result) {

    for (int i = 0; i < result.size() -1; i++) {
        if (result.get(i+1).getDigit() > result.get(i).getDigit()) {


            int indx = inorder.indexOf(0);
            System.out.println(indx);


        }
    }

}

public static void main(String[] args){
    List<Element> listTwo = new ArrayList();
     List<Element> listOne = new ArrayList();
    Element e = new Element(0, 0.12);
    Element e1 = new Element(2, 0.13);
    Element e3 = new Element(3,0.5);
    listTwo.add(e);
    listTwo.add(e1);
    listTwo.add(e3);
    listOne.add(e);
    listOne.add(e1);
    listOne.add(e3);

    Collections.sort(listOne,new SortingObjectsWithDigitField());

    new NewClass(listTwo,listOne);
}
}

Upvotes: 0

Views: 97

Answers (1)

unholysampler
unholysampler

Reputation: 17331

Your list is a list of Element objects. You are looking for the index of the integer 0. An integer will never be equal to an Element, therefor indexOf() does not find 0 and returns -1 to indicate that it was not found.

Upvotes: 2

Related Questions