Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I'm having a problem with this. It's only printing once, making a comparison between H < i. Help? public static ArrayList organizePrefixOrder(ArrayList a, ArrayList b)
I'm having a problem with this. It's only printing once, making a comparison between H < i. Help? public static ArrayList organizePrefixOrder(ArrayList a, ArrayList b) { int compared; if (a.size() == 0) { b.add(b.size(), a.get(0)); return b; } else { compared = byPrefixOrder().compare(a.get(0), a.get(1)); System.out.println(compared); if (compared > 1) { b.add(0, a.get(1)); a.remove(1); return organizePrefixOrder(a,b); } else if (compared == 1) { b.add(1, a.get(0)); b.add(2, a.get(1)); a.remove(0); a.remove(1); return organizePrefixOrder(a,b); } else { b.add(a.get(0)); a.remove(0); return organizePrefixOrder(a,b); } } }
public static Comparator byPrefixOrder() { return new ComparatorByPrefixOrder(); }
private static class ComparatorByPrefixOrder implements Comparator { @Override public int compare(Term a, Term b) { if (a.getSearched().charAt(0) == b.getSearched().charAt(0)) { System.out.println(a.getSearched().charAt(0) + "=" + b.getSearched().charAt(0)); return 0; } else if (a.getSearched().charAt(0) > b.getSearched().charAt(0)) { System.out.println(a.getSearched().charAt(0) + ">" + b.getSearched().charAt(0)); return -1; } else System.out.println(a.getSearched().charAt(0) + "<" + b.getSearched().charAt(0)); return 1; } }
Error:
1 How to Get Away with Murder
Exception in thread "main" 2 iZombie
3 Breaking Bad
8 Schussler Sucks More
4 Big Bang Theory
H
1
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665)
at java.util.ArrayList.add(ArrayList.java:477)
at Term.organizePrefixOrder(Term.java:77)
at Term.main(Term.java:175)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started