int m = 5;
int [] a = new int[5];
//Asignamos un valor a la posicion 1
A[1] = 2;
B[2] = a[1];
a[0] = a[1] + a[2] + 2;
a[0]++;
a[3] = m + 10;
Por ejemplo si queremos digitar nuevo en un vector
| 1 | 2 | 3 | 5 | 6 |
|---|
Digite el número 4 sin que se elimine el digito colocado en su posición, y que el digito se pase a otra posición del vector.
| 1 | 2 | 3 | 4 | 5 | 6 |
|---|
La posición es el índice de un vector.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package a;
/**
*
* @author LAB-USR-CHIM-A0202
*/
import java.lang.*;
import java.util.Scanner;
public class ae {
static int[] vec = new int[100];
static int n;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
int dato;
n = leerLongitud();
llenarAleatoriosVector(10);
imprimirVector();
dato = leerDato();
adicionarDatoVector(dato);
ActualizarVector();
imprimirVector();
}
public static void adicionarDatoVector(int dato) {
vec[n] = dato;
n++;
}
public static int leerLongitud() {
Scanner scan = new Scanner(System.in);
int tam;
do {
System.out.print("Ingrese el tamaño del vector: ");
tam
= scan.nextInt();
} while (tam < 1);
return tam;
}
public static int leerDato() {
Scanner scan = new Scanner(System.in);
int nro;
do {
System.out.print("Ingrese el numero para adicionar: ");
nro = scan.nextInt();
} while (nro < 1);
return nro;
}
public static void llenarAleatoriosVector(int nro) {
int i;
for (i = 0; i < n; i++) {
vec[i] = (int) (Math.random() * nro + 1);
}
}
public static void imprimirVector() {
int i;
System.out.print("v[");
for (i = 0; i < n - 1; i++) {
System.out.print(vec[i] + ",");
}
System.out.println(vec[i] + "]");
}
public static void ActualizarVector() {
Scanner leer = new Scanner(System.in);
System.out.println("Quieres actualizar alguno? S/N");
String respuesta = leer.next();
int posicion_actualizar;
if (respuesta.equals("S")) {
System.out.println("que posicion del arreglo quieresactualizar ?");
posicion_actualizar = leer.nextInt();
System.out.println("Escribe el nuevo valor en la poscion "
+ posicion_actualizar);
vec[posicion_actualizar] = leer.nextInt(); //valornuevo=leer.nextInt();
}
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package a;
import java.util.Scanner;
/**
*
* @author LAB-USR-CHIM-A0202
*/
public class Adicionar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner entrada = new Scanner(System.in);
int arreglo[] = new int[10];
boolean creciente = true;
int numero, sitio_num = 0, j = 0;
System.out.println("Llenar el arreglo");
do {
//1lenando el arreglo
for (int i = 0; i < 5; i++) {
System.out.print((i + 1) + ". Digite un numero: ");
arreglo[i] = entrada.nextInt();
}
//comprobar si el arreglo esta ordenado en orden creciente
for (int i = 0; i < 4; i++) {
if (arreglo[i] < arreglo[i + 1]) { //Creciente: 1-2-3 creciente = true;
creciente = true;
}
if (arreglo[i] > arreglo[i + 1]) {
//Decreciente: 3-2-1
creciente = false;
break;
}
}
if (creciente == false) {
System.out.println("\\nEl arreglo no esta ordenado en forma creciente, vuelva a ingresar");
}
} while (creciente == false);
System.out.print("\\nDigite un elemento a insertar: ");
numero = entrada.nextInt();
//Esto es para darnos cuenta en que posicion va el numero
while (arreglo[j] < numero && j < 5) {
sitio_num++;
j++;
}
//Por ultimo, trasladamos una posicion en el arreglo a los elementos que van detras de
for (int i = 4; i >= sitio_num; i--) {
arreglo[i + 1] = arreglo[i];
}
//insertamos el numero que el usuario puso
arreglo[sitio_num] = numero;
System.out.print("\\nEl arreglo queda: ");
for (int i = 0; i < 6; i++) {
System.out.print(arreglo[i] + " - ");
}
System.out.println();
}
}
https://www.youtube.com/watch?v=FEj1MdFpHG8
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package a;
/**
*
* @author LAB-USR-CHIM-A0202
*/
import java.lang.*;
public class oa {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Este codigo es solo para imprmir el vector
int[] vec = {7, 23, 6, 8, 10, 45};
int i;
for (i = 0; i < vec.length; i++) {
System.out.print(vec[i] + ",");
}
}
}