allora....giusto per....questo e` il mio input:
codice:
Inserisci la lunghezza dell'array
5
Inserisci [1] valore del titolo:
1,2
Inserisci [2] valore del titolo:
-3,4
Inserisci [3] valore del titolo:
-2,2
Inserisci [4] valore del titolo:
2,5
Inserisci [5] valore del titolo:
1,1
1.2 -3.4 -2.2 2.5 1.1
-3.4 -2.2
-2.2 -3.4
e questo e` il codice
codice:
package ssc.energy.enipower.controller;
import java.util.Scanner;
public class JavaApplication23 {
static Scanner scanner = new Scanner(System.in);
public static void main(String... args) {
System.out.println("Inserisci la lunghezza dell'array");
double vettore[] = creoArray(scanner.nextInt());
stampaVettore(vettore);
double[] vettoreElementiNegativi = creaVettoreElementiNegativi(vettore);
stampaVettore(vettoreElementiNegativi);
ordinaVettore(vettoreElementiNegativi);
stampaVettore(vettoreElementiNegativi);
}
private static double[] creaVettoreElementiNegativi(double[] vettore) {
double[] newVettore = new double[contaElementiNegativi(vettore)];
int k = 0;
for (int index = 0; index < vettore.length; index++) {
if (vettore[index] < 0) {
newVettore[k] = vettore[index];
k++;
}
}
return newVettore;
}
private static void ordinaVettore(double[] array) {
for (int i = 0; i < array.length; i++) {
boolean flag = false;
for (int j = 0; j < array.length - 1; j++) {
if (array[j] < array[j + 1]) {
double k = array[j];
array[j] = array[j + 1];
array[j + 1] = k;
flag = true;
}
}
if (!flag)
break;
}
}
private static void stampaVettore(double[] vettore) {
for (int index = 0; index < vettore.length; index++) {
System.out.print(vettore[index]);
if (index != vettore.length - 1)
System.out.print(" ");
}
System.out.println();
}
private static int contaElementiNegativi(double[] vettore) {
int counter = 0;
for (int index = 0; index < vettore.length; index++) {
if (vettore[index] < 0)
counter++;
}
return counter;
}
private static double[] creoArray(int dimensione) {
double vettore[] = new double[dimensione];
for (int index = 0; index < vettore.length; index++) {
System.out.println("Inserisci [" + (index + 1) + "] valore del titolo: ");
vettore[index] = scanner.nextDouble();
}
return vettore;
}
}
ovviamente ci sono miliardi di modi per farlo meglio...usando le collection, scrivendo codice ottimizzato...ma non credo che in questo caso si cerchi questo risultato!!!