Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tp2;
- import java.util.ArrayList;
- import java.lang.String;
- /**
- *
- * @author francis
- */
- public class SortedListOfStrings extends ArrayList{
- /**
- * @param args the command line arguments
- */
- public SortedListOfStrings(){
- super();
- }
- public void add(String chaine){
- if (this.isEmpty())
- add( 0, chaine);
- if (this.contains(chaine) != true){
- int i = 0;
- String objet_courant = (String) this.get(i);
- while(i != this.size() && chaine.compareTo(objet_courant) < 0){
- objet_courant = (String) this.get(i);
- i++;
- }
- add( i, chaine);
- }
- }
- public static void main(String[] args) {
- // TODO code application logic here
- String ch0 = "z";
- String ch1 = "a";
- String ch2 = "c";
- String ch3 = "e";
- String ch4 = "f";
- SortedListOfStrings chaine_triée = new SortedListOfStrings();
- chaine_triée.add(ch0);
- chaine_triée.add(ch1);
- /*chaine_triée.add(ch2);
- chaine_triée.add(ch3);
- chaine_triée.add(ch4);*/
- for (int i = 0; i < chaine_triée.size(); i++){
- System.out.println(chaine_triée.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement