1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public class Sequence {
public boolean testBalance(ArrayList toTest){
boolean test=false;
ArrayList copyList=new ArrayList();
//si vide il est balancée
if(toTest.size()==0){
test=true;
}else{
if(toTest.size() % 2 != 0){
//s'il n'est pas divisible par 2 la séq. est forcément non balancée
test=false;
}else{
for(int i=0; i<toTest.size(); i++){
if(toTest.get(i).equals("(") && toTest.get(i+1).equals(")")){
i++;
}else{
copyList.add(toTest.get(i));
}
}
test=testBalance(copyList);
}
}
return test;
}
public static void main(String[] args) {
//String [] toTest={"(","(",")","(",")",")","(",")"};
String [] toTest={"(","(","(",")","(",")",")",")"};
//String [] toTest={"(","(",")"};
//String [] toTest={"(",")","(",")"};
ArrayList list= new ArrayList(Arrays.asList(toTest));
System.out.println(list);
Sequence examen= new Sequence();
System.out.println(examen.testBalance(list));
}
} |
Partager