#ConcatArray System#

0 respostas
U

Espero que você gostem, vizualizando o tópico anterior criei uma classe para facilitar a concatenação de vetores inteiros.

Lembrando que para colocar mais vetores, basta utilizar o mesmo processo da segunda precedência.

/** #ConcatArray System#
Copyright ©2011 - @uthor #Utroz#. */

public class ConcatArray {
	private int[] array;	// Array for reference.
	private int[] newArray; 	// The concatenate array.	

	public static int cont = 0; //Current position of array.
	
	public ConcatArray(int[] args){
		this.array = args;
		newArray = new int[args.length];
	}	

	public void sendArray(int[] args){
		this.array = args;
	}
	
	public void concatenate() {	
			/* Concatenate array. */
			for(int i = 0; i < array.length; i++){
				newArray[cont++] = array[i];
			}
	}
	
	public void recreateArray(){
		int[] recreateArray = new int[array.length + newArray.length];
		int ref = 0; // Reference variable.
		
        // Copy the old array to 'recreateArray'.	
		for(int i = 0; i < newArray.length; i++){
			recreateArray[ref++] = newArray[i];
		}
		
		this.newArray = recreateArray;
	}
	
	public void print(){
		for(int print: newArray){
			System.out.println(print);
		}
	}
}

class Run {	
	public static void main(String[] args){	
		/* first precedence. */
		int[] firstArray = {5,4,3,2,1}; 	
		ConcatArray obj = new ConcatArray(firstArray);	// Send the first array for instance.	
		obj.concatenate(); // Concatenate on the 'newArray'.
		
		/* second precedence. */
		int[] secondArray = {10,9,8,7,6};
		obj.sendArray(secondArray); // 
		obj.recreateArray(); // This is needed because of array size.
		obj.concatenate(); // Concatenate on the 'newArray'.
		
		/* third precedence. */
		obj.print(); // print the 'newArray'.
		
		/* Result: [5, 4, 3, 2, 1, 10, 9, 8, 7, 6]. */
	}
}

Att, Utroz.

Criado 20 de junho de 2011
Respostas 0
Participantes 1