mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(ch 4): blocks 1 & 2 solved
This commit is contained in:
@@ -2,24 +2,184 @@ package tutorialJava;
|
|||||||
|
|
||||||
public class UtilsArrays {
|
public class UtilsArrays {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param a
|
|
||||||
*/
|
|
||||||
public static void inicializarArray(int a[], int limiteSup) {
|
|
||||||
for (int i = 0; i < a.length; i++) {
|
|
||||||
a[i] = (int) Math.round(Math.random() * limiteSup);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Imprime el array en consola, de forma que queda en una única línea y con
|
||||||
* @param a
|
* un espacio en blanco entre valores
|
||||||
|
* @param num
|
||||||
*/
|
*/
|
||||||
public static void muestraArray(int a[]) {
|
public static void imprimeArray (int a[]) {
|
||||||
for (int i = 0; i < a.length; i++) {
|
for (int i = 0; i < a.length; i++) {
|
||||||
System.out.print(a[i] + " ");
|
System.out.print(a[i] + " ");
|
||||||
}
|
}
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Imprime el array en consola, de forma que queda en una única línea y con
|
||||||
|
* un espacio en blanco entre valores
|
||||||
|
* @param num
|
||||||
|
*/
|
||||||
|
public static void imprimeArray (char a[]) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
System.out.print(a[i] + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param limInf
|
||||||
|
* @param limSup
|
||||||
|
*/
|
||||||
|
public static void inicializaArray (int a[], int limInf, int limSup) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
a[i] = Utils.obtenerNumeroAzar(limInf, limSup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int suma (int array[]) {
|
||||||
|
int suma = 0;
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
suma += array[i];
|
||||||
|
}
|
||||||
|
return suma;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static float promedia (int array[]) {
|
||||||
|
return suma(array) / (float) array.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int mayor (int array[]) {
|
||||||
|
int mayor = array[0];
|
||||||
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
if (array[i] > mayor) mayor = array[i];
|
||||||
|
}
|
||||||
|
return mayor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int menor (int array[]) {
|
||||||
|
int menor = array[0];
|
||||||
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
if (array[i] < menor) menor = array[i];
|
||||||
|
}
|
||||||
|
return menor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param filas
|
||||||
|
* @param cols
|
||||||
|
* @param valorInicial
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[][] creaEInicializaMatriz (int filas, int cols, int valorInicial) {
|
||||||
|
int m[][] = new int[filas][cols];
|
||||||
|
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
m[i][j] = valorInicial;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param filas
|
||||||
|
* @param cols
|
||||||
|
* @param minimo
|
||||||
|
* @param maximo
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[][] creaEInicializaMatrizAlAzar (
|
||||||
|
int filas, int cols, int minimo, int maximo) {
|
||||||
|
|
||||||
|
int m[][] = new int[filas][cols];
|
||||||
|
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
m[i][j] = Utils.obtenerNumeroAzar(minimo, maximo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
*/
|
||||||
|
public static void imprimeMatriz (int m[][]) {
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
System.out.print(m[i][j] + "\t");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void ordenaArray (int a[], boolean asc) {
|
||||||
|
boolean hayIntercambios;
|
||||||
|
do {
|
||||||
|
hayIntercambios = false;
|
||||||
|
for (int i = 0; i < (a.length - 1); i++) {
|
||||||
|
if ((asc == true && a[i] > a[i + 1])
|
||||||
|
|| (asc == false && a[i] < a[i + 1])) {
|
||||||
|
int aux = a[i];
|
||||||
|
a[i] = a[i + 1];
|
||||||
|
a[i + 1] = aux;
|
||||||
|
hayIntercambios = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (hayIntercambios == true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ordenaArray(int[] a) {
|
||||||
|
ordenaArray(a, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays;
|
||||||
|
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ej03_ArraysDeCaracteres {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// char a[] = new char[] {'H', 'o', 'l', 'a'};
|
||||||
|
//
|
||||||
|
// for (int i = 0; i < a.length; i++) {
|
||||||
|
// System.out.println((int) a[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// int a[] = new int[10];
|
||||||
|
// UtilsArrays.inicializaArray(a, 97, 122);
|
||||||
|
// for (int i = 0; i < a.length; i++) {
|
||||||
|
// System.out.print(a[i]);
|
||||||
|
// System.out.println(" " + ((char) a[i]));
|
||||||
|
// }
|
||||||
|
|
||||||
|
String str = "HolA";
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
int valorNumChar = (int) str.charAt(i);
|
||||||
|
if (valorNumChar >= 97 && valorNumChar <=122) { // minúsculas
|
||||||
|
System.out.print(str.charAt(i));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (valorNumChar >= 65 && valorNumChar <= 90) { // mayúsculas
|
||||||
|
System.out.print( (char) (valorNumChar + 32) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -6,8 +6,8 @@ public class EjemploMetodos {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int array[] = new int[10];
|
int array[] = new int[10];
|
||||||
UtilsArrays.inicializarArray(array, 10000);
|
UtilsArrays.inicializaArray(array, 0, 10000);
|
||||||
UtilsArrays.muestraArray(array);
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio01_150ElementosEnArray_SumaMediaMayorMenor {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[500];
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, 0, 100);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
|
||||||
|
// Imprimo resultados
|
||||||
|
System.out.println("Suma: " + UtilsArrays.suma(array)
|
||||||
|
+ " - Media: " + UtilsArrays.promedia(array) +
|
||||||
|
" - Menor: " + UtilsArrays.menor(array)
|
||||||
|
+ " - Mayor: " + UtilsArrays.mayor(array));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio02_150ElementosEnArray_SumaMediaMayorMenorEntreLimites {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[5], suma = 0, mayor, menor;
|
||||||
|
int limInf, limSup;
|
||||||
|
float media;
|
||||||
|
|
||||||
|
limInf = Utils.pideNumeroEntero("Introduzca límite inferior: ");
|
||||||
|
limSup = Utils.pideNumeroEntero("Introduzca límite superior: ");
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, limInf, limSup);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
// Imprimo resultados
|
||||||
|
System.out.println("Suma: " + UtilsArrays.suma(array)
|
||||||
|
+ " - Media: " + UtilsArrays.promedia(array) +
|
||||||
|
" - Menor: " + UtilsArrays.menor(array)
|
||||||
|
+ " - Mayor: " + UtilsArrays.mayor(array));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio03_MuestraArrayEnOrdenInverso {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[5];
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, 0, 100);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
for (int i = array.length-1; i >= 0; i--) {
|
||||||
|
System.out.print(array[i] + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio04_EncuentraElementoEnArray {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[5];
|
||||||
|
int numBuscado;
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, 0, 100);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
numBuscado = Utils.pideNumeroEntero("Número que deseas encontrar: ");
|
||||||
|
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
if (numBuscado == array[i]) {
|
||||||
|
System.out.println("Encontrado en posición " + i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("El número no existe en el array");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio05_SumaValoresParesEImpares {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[5];
|
||||||
|
int sumaPares = 0, sumaImpares = 0;
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, 0, 100);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
if (array[i] % 2 == 0) {
|
||||||
|
sumaPares += array[i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sumaImpares += array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Suma pares: " + sumaPares + " - "
|
||||||
|
+ "Suma impares: " + sumaImpares);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque01;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio06_SumaValoresIndicePar {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int array[] = new int[5];
|
||||||
|
int sumaPares = 0, sumaImpares = 0;
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(array, 0, 100);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(array);
|
||||||
|
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
if (array[i] % 2 == 0) {
|
||||||
|
sumaPares += array[i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sumaImpares += array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Suma pares: " + sumaPares);
|
||||||
|
System.out.println("Suma impares: " + sumaImpares);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio01_ParesCambianSignoEnArray {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a[] = new int[15];
|
||||||
|
UtilsArrays.inicializaArray(a, -100, 100);
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
cambiaSignoEnValores(a);
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
*/
|
||||||
|
public static void cambiaSignoEnValores(int array[]) {
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
if (array[i] % 2 == 0) {
|
||||||
|
array[i] = -array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio02_DosArraysCrearTercerArray {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a1[] = new int[15];
|
||||||
|
int a2[] = new int[15];
|
||||||
|
int a3[];
|
||||||
|
|
||||||
|
UtilsArrays.inicializaArray(a1, 0, 100);
|
||||||
|
UtilsArrays.imprimeArray(a1);
|
||||||
|
UtilsArrays.inicializaArray(a2, 0, 100);
|
||||||
|
UtilsArrays.imprimeArray(a2);
|
||||||
|
|
||||||
|
a3 = creaTercerArray(a1, a2);
|
||||||
|
UtilsArrays.imprimeArray(a3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
*/
|
||||||
|
public static int[] creaTercerArray (int a1[], int a2[]) {
|
||||||
|
int a[] = new int[a1.length];
|
||||||
|
for (int i = 0; i < a1.length; i++) {
|
||||||
|
if (i % 2 == 1) {
|
||||||
|
a[i] = a1[i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
a[i] = a2[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio03_MultiplicaElementosArray {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a1[] = new int[15];
|
||||||
|
int factor;
|
||||||
|
|
||||||
|
// Incializo e imprimo el array
|
||||||
|
UtilsArrays.inicializaArray(a1, 0, 100);
|
||||||
|
UtilsArrays.imprimeArray(a1);
|
||||||
|
|
||||||
|
factor = Utils.pideNumeroEntero("Por favor, introduzca el factor: ");
|
||||||
|
|
||||||
|
multiplicaValoresPorFactor(a1, factor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param factor
|
||||||
|
*/
|
||||||
|
public static void multiplicaValoresPorFactor (int a[], int factor) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
a[i] *= factor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio04_DeplazamientoCiclicoDerecha {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a[] = new int[] {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
desplazaCiclicoDerecha(a);
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param factor
|
||||||
|
*/
|
||||||
|
public static void desplazaCiclicoDerecha (int a[]) {
|
||||||
|
int aux = a[a.length - 1];
|
||||||
|
for (int i = (a.length - 1); i > 0; i--) {
|
||||||
|
a[i] = a[i - 1];
|
||||||
|
}
|
||||||
|
a[0] = aux;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio05_DeplazamientoCiclicoDerechaLugaresPersonalizados {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a[] = new int[] {1, 2, 3, 4, 5};
|
||||||
|
int posiciones;
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
|
||||||
|
posiciones = Utils.pideNumeroEntero("Indica cantidad de posiciones a derecha: ");
|
||||||
|
desplazaCiclicoDerecha(a, posiciones);
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param factor
|
||||||
|
*/
|
||||||
|
public static void desplazaCiclicoDerecha (int a[], int posicionesARotar) {
|
||||||
|
for (int repeticiones = 0; repeticiones < posicionesARotar; repeticiones++) {
|
||||||
|
int aux = a[a.length - 1];
|
||||||
|
for (int i = (a.length - 1); i > 0; i--) {
|
||||||
|
a[i] = a[i - 1];
|
||||||
|
}
|
||||||
|
a[0] = aux;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package tutorialJava.capitulo4_Arrays.ejercicios.bloque02;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ejercicio06_DeplazamientoCiclicoLugaresYDireccionPersonalizadas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main (String args[]) {
|
||||||
|
int a[] = new int[] {1, 2, 3, 4, 5};
|
||||||
|
int posiciones, direccion;
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
|
||||||
|
posiciones = Utils.pideNumeroEntero("Indica cantidad de posiciones: ");
|
||||||
|
direccion = Utils.pideNumeroEntero("Indica dirección (0-Izquierda, 1-Derecha): ");
|
||||||
|
desplazaCiclico(a, posiciones, direccion);
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param factor
|
||||||
|
*/
|
||||||
|
public static void desplazaCiclico (int a[], int posiciones, int direccion) {
|
||||||
|
|
||||||
|
if (direccion == 1) { // Se desea desplazar a derecha
|
||||||
|
for (int repeticiones = 0; repeticiones < posiciones; repeticiones++) {
|
||||||
|
int aux = a[a.length - 1];
|
||||||
|
for (int i = (a.length - 1); i > 0; i--) {
|
||||||
|
a[i] = a[i - 1];
|
||||||
|
}
|
||||||
|
a[0] = aux;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // Se desea desplazar a izquierda
|
||||||
|
for (int repeticiones = 0; repeticiones < posiciones; repeticiones++) {
|
||||||
|
int aux = a[0];
|
||||||
|
for (int i = 0; i < (a.length - 1); i++) {
|
||||||
|
a[i] = a[i + 1];
|
||||||
|
}
|
||||||
|
a[a.length - 1] = aux;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user