Elenco di array e ricerca della sottosequenza più lunga con lo stesso numero [chiuso]

StackOverflow https://stackoverflow.com//questions/22077983

  •  23-12-2019
  •  | 
  •  

Domanda

Mi chiedevo quale sarebbe stato il modo migliore per implementare questo.

Non riesco a pensare a un buon modo per salvare quali informazioni devono essere salvate come l'indice e il numero di valori e infine il numero effettivo che viene ripetuto

public class testing 
{

public static void main(String[] args) 
{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    Integer a =0;
    Integer value = 0;
    Integer num = 0;

    boolean loop = true;
    //getting the string information
    while(loop)

    {
        System.out.println("Enter a series of numbers, 0 to stop");
        Integer n = in.nextInt();
        if(n.equals(0))
        {
            break;
        }
        else
        { 
            numbers.add(n);         

        }



    }

    for (int i = 1; i < numbers.size(); i++)
    { 




    }

}



}
È stato utile?

Soluzione

Potresti usare un ArrayList 2d, dichiarato in questo modo:

ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

e quindi dichiarare i 2 ArrayList da aggiungere ad esso alla fine del processo:

ArrayList<Integer> length = new ArrayList<Integer>();

ArrayList<Integer> value = new ArrayList<Integer>();

Poi

1) scorrere l'elenco controllando se l'elemento è lo stesso del precedente.

Se lo è, continua fino alla fine o viene scoperto un elemento che differisce, a quel punto memorizza il numero degli elementi uguali precedenti nell'ArrayList chiamato 'length' e il valore dell'elemento in quello chiamato 'value'.Avere un int (chiamato index say) che memorizza l'indice dell'elemento in lunghezza contenente la lunghezza della sottosequenza corrente più lunga (che sarà uguale all'indice dell'elemento contenente il valore dell'elemento di cui è composto (che è stato memorizzato in valore)).

Se non lo è, passare all'elemento successivo.

2) Ripetere il processo, aggiornando l'indice se necessario (es.se viene scoperta una sottosequenza più lunga).

Per aggiungere lunghezza e valore al risultato alla fine, basta fare result.add(length); e result.add(value);

Se si desidera restituire un oggetto che contiene tutte le informazioni richieste, è possibile avvolgere l'int 'index' in un numero intero e aggiungerlo alla fine dell'ArrayList chiamato 'length' o addirittura inserirlo in un nuovo ArrayList e aggiungere quell'ArrayList al risultato.

Si noti che per recuperare un elemento all'indice i nel primo ArrayList (in questo caso quello chiamato 'length') dopo che è stato memorizzato nel risultato, è necessario fare

result.get(0).get(i);

MODIFICARE:

Quindi la parte for loop che avevo in mente sarebbe questa:

boolean same = false;
int sequenceLength = 0;
Integer sequenceInteger = null; 

for (int i = 1; i < numbers.size(); i++)
        { 
            if(numbers.get(i).equals(numbers.get(i-1)))
                {
                      same = true;
                      sequenceLength++;
                }      
            else(if same == true)
                {
                      sequenceInteger = new Integer(sequenceLength);
                      //add sequenceInteger to length and numbers.get(i-1) to value 
                      same = false;
                      sequenceLength = 0;
                }
            // else do nothing since same is false, which means that the current
            // element is different from the previous and the previous is 
            // different the one before that, so there are no new values to store
        }
// end of list reached
(if same == true)
{
      sequenceInteger = new Integer(sequenceLength);
      //add sequenceInteger to length and numbers.get(i-1) to value 
      same = false;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top