Mettre le contenu d'une liste dans un tableau

Fermé
rimja Messages postés 5 Date d'inscription lundi 29 mai 2017 Statut Membre Dernière intervention 31 mai 2017 - 31 mai 2017 à 03:07
rimja Messages postés 5 Date d'inscription lundi 29 mai 2017 Statut Membre Dernière intervention 31 mai 2017 - 31 mai 2017 à 16:56
Bonjour
j'utilise se code pour créer une liste ordonnée maintenant je veux récupérer le contenu de cette liste dans un tableau




		List<Cvs> listCvs = new LinkedList<Cvs>();


		SequentialBehaviour comportementSequentiel = new SequentialBehaviour();
		
		comportementSequentiel.addSubBehaviour(new OneShotBehaviour(){
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public void action()  {
				
				

				
				
				try {
				
				
					Class.forName("org.relique.jdbc.csv.CsvDriver");

					Properties props = new Properties();
					props.put("fileExtension", ".txt");

					Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:\\csv", props);

					Statement stmt = conn.createStatement();

					List<Cvs> listCvs = new LinkedList<Cvs>();
					ResultSet results = stmt.executeQuery("select * from csv order by eo");

					while (results.next()) {
						int id = results.getInt("id");
						String origin = results.getString("origin");
						String destination = results.getString("destination");
						int capacite = results.getInt("capacite");
						String eo = results.getString("eo");
						String em = results.getString("Em");
						String dtm = results.getString("DTM");
						String dtd = results.getString("DTD");
						String lo = results.getString("lo");
						String ed = results.getString("ed");
						String ld = results.getString("ld");
						Cvs cvs = new Cvs(id, origin, destination, capacite, eo, em, dtm, dtd, lo, ed, ld);
						listCvs.add(cvs);
					}
					
					Collections.sort(listCvs, new Comparator() {
						@Override
						public int compare(Object CvsOne, Object CvsTwo) {
							return ((Cvs) CvsOne).getEo().compareTo(((Cvs) CvsTwo).getEo());
						}
					});

				    
				 
				for (Cvs str : listCvs) {
						System.out.println(str.getId() + " | " + str.getOrigin() + " | " + str.getDestination() + " | "
								+ str.getCapacite() + " | " + str.getEo() + " | " + str.getEm() + " | " + str.getDTM() + " | "
								+ str.getDTD() + " | " + str.getLo() + " | " + str.getEd() + " | " + str.getLd());
						
						
						
					}
					
					
					
				
				


1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
31 mai 2017 à 07:09
Bonjour,

List a une méthode toArray.

String[] array = list.toArray(new String[list.size()]);

Attention : tu as choisis une LinkedList pour implémenter ta liste, mais en terme de performances l'opération de tri Collections.sort va coûter très cher.
Je t'invite à d'abord convertir ta liste en tableau et après faire le tri avec Arrays.sort afin d'améliorer les temps de traitement.
0
Comment ? Je trie un tableau ?
J'ai pas bien compris
0
rimja Messages postés 5 Date d'inscription lundi 29 mai 2017 Statut Membre Dernière intervention 31 mai 2017
31 mai 2017 à 16:56
Comment je converti LinkedList en tableau ?
0