mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-10 02:13:07 +01:00
feat(chapter to 4): added
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ej01_EjemploArraysPrimitivos {
|
||||
|
||||
/**
|
||||
* Método principal
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
primerEjemploArray();
|
||||
// segundoEjemploArray();
|
||||
// tercerEjemploArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ejemplo de declaración de un array, inicialización de valores al azar y mostrar el array en consola
|
||||
*/
|
||||
public static void primerEjemploArray () {
|
||||
// Declaración del array, a través de la sentencia "new"
|
||||
int numeros[] = new int[5];
|
||||
|
||||
// Inicialización de los valores del array
|
||||
for (int i = 0; i < numeros.length; i++) {
|
||||
numeros[i] = (int) Math.round(Math.random() * 100);
|
||||
}
|
||||
|
||||
// Recorrido del array para imprimir sus valores en pantalla
|
||||
for (int i = 0; i < numeros.length; i++) {
|
||||
System.out.print(numeros[i] + " ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ejemplo de una segunda forma de crear un array, en este caso lo creamos con unos valores directamente, en
|
||||
* lugar de inicializarlo con valores al azar.
|
||||
*/
|
||||
public static void segundoEjemploArray() {
|
||||
// Otra forma de inicializar el array
|
||||
int array2[] = new int[] {88, 89, 90, 4, 5, 6, 7, 8, 9, 10};
|
||||
|
||||
// Recorrido del array para imprimir sus valores en pantalla.
|
||||
// Este tipo de bucle se conoce como "for each".
|
||||
for (int valorDentroDeArray : array2) {
|
||||
System.out.println(valorDentroDeArray);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Método que crea e inicializa un array con 100 elementos al azar entre 0 y 100.
|
||||
* Después calcula la suma, la media, el mayor y el menor de todos.
|
||||
*/
|
||||
public static void tercerEjemploArray() {
|
||||
int suma = 0, mayor, menor;
|
||||
float media;
|
||||
|
||||
// Declaración del array
|
||||
int array[] = new int[1000000];
|
||||
|
||||
// Recorro para inicializar el array
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = (int) Math.round(Math.random()*10000);
|
||||
}
|
||||
|
||||
// Recorro e imprimo en pantalla
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.print(array[i] + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
// Recorro para obtener la suma
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
suma += array[i];
|
||||
}
|
||||
// Calculo la media
|
||||
media = suma / ((float) array.length);
|
||||
|
||||
// Mayor y menor
|
||||
mayor = array[0];
|
||||
menor = array[0];
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
if (array[i] > mayor) mayor = array[i];
|
||||
if (array[i] < menor) menor = array[i];
|
||||
}
|
||||
|
||||
// Impresión de resultados
|
||||
System.out.println("suma: " + suma + " - media: " + media +
|
||||
" - mayor: " + mayor + " - menor: " + menor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
public class Ej02_OrdenacionDeUnArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// int array[] = new int[] {50, 100, 23, 2, 48, 66, 4};
|
||||
int array[] = new int[100];
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = (int) Math.round(Math.random() * 1000);
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.println("Voy con la posición i=" + i);
|
||||
// Simplemente muestro el array
|
||||
System.out.print("El array está así: ");
|
||||
for (int j = 0; j < array.length; j++) {
|
||||
System.out.print(array[j] + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
|
||||
// Empieza el algoritmo de ordenación de un intervalo desde
|
||||
// el valor de "i" hasta el final del array
|
||||
int indiceMenor = i;
|
||||
for (int j = i+1; j < array.length; j++) {
|
||||
if (array[j] < array[indiceMenor]) {
|
||||
indiceMenor = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Intercambio de variables
|
||||
if (i != indiceMenor) {
|
||||
int aux = array[i];
|
||||
array[i] = array[indiceMenor];
|
||||
array[indiceMenor] = aux;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
30
src/tutorialJava/capitulo4_Arrays/Ej03_EjemploMatrices.java
Normal file
30
src/tutorialJava/capitulo4_Arrays/Ej03_EjemploMatrices.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Ej03_EjemploMatrices {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int m[][] = new int[4][3];
|
||||
int m2[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
|
||||
|
||||
int cont = 1;
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
// m[i][j] = (int) Math.round(Math.random() * 100);
|
||||
m[i][j] = cont;
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
|
||||
m[0][2] = 1;
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Ej04_ComoHacerEjerciciosMatrices {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int m[][] = new int[][] { {1, 2, 3, 4, 0},
|
||||
{5, 6, 0, 8, 9},
|
||||
{10, 0, 12, 13, 14},
|
||||
{0, 16, 17, 18, 19},
|
||||
{20, 21, 22, 0, 24} };
|
||||
|
||||
// inicializaMatrizAlAzar(m);
|
||||
imprimeMatriz(m);
|
||||
System.out.println("Es positiva: " + esMatrizPositiva(m));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param m
|
||||
*/
|
||||
public static void inicializaMatrizAlAzar(int m[][]) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = (int) Math.round(Math.random() * 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param m
|
||||
* @return
|
||||
*/
|
||||
public static boolean esMatrizPositiva(int m[][]) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
if (m[i][j] < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Ej05_InicializarMatrizSinRepetirValores {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int m[][] = new int[5][5];
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
int num;
|
||||
do {
|
||||
num = Utils.obtenerNumeroAzar(0, 100);
|
||||
} while (valorEstaEnMatriz(num, m));
|
||||
m[i][j] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean valorEstaEnMatriz (int valor, int m[][]) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
if (valor == m[i][j]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user