Substituindo um Vetor … Inserindo um novo Valor…
public static void main(String args[])
{
int i = 0, tam = 0, cont = 0;
int num[] = new int[100];
int pos, valor;
Scanner sc = new Scanner(System.in);
System.out.printf("\n\n\n----- Para Finalizar Vetor Digite < 0 >-----\n\n");
System.out.print("Entre com os Valores do Vetor: ");
do
{
num[i] = sc.nextInt();
cont++;
i++;
} while(num[i - 1] != 0);
System.out.printf("\n\nExibindo o Vetor Digitado ");
for(int k = 0; k < (cont - 1); k++)
{
System.out.print(num[k] + " ");
}
System.out.print("\n\n(Atencao)Substitua a Posicao do Vetor entre " + "0 a " + (cont - 2) + ": ");
pos = sc.nextInt();
System.out.print("\n\nInsira um Novo numero no Vetor: ");
valor = sc.nextInt();
SubVetor(num, tam, pos, valor);
System.out.printf("\n\nExibindo o novo Vetor... ");
for(int k = 0; k < (cont - 1); k++)
{
System.out.print(num[k] + " ");
}
System.out.printf("\n");
}
public static void SubVetor(int v[], int tam, int pos, int valor)
{
int size;
size = v.length - 1;
if(pos >= 0 && pos <= tam && tam <= size)
for(int k = tam; k > pos; k--)
v[k + 1] = v[k];
tam++;
v[pos] = valor;
}
é bem simples …sei que da pra melhorar …mas funcionou aqui Avaliem…
Exemplo
[b]----- Para Finalizar Vetor Digite < 0 >-----
Entre com os Valores do Vetor: 12 23 35 56 67 789 21 4 9 34 66 0
Exibindo o Vetor Digitado 12 23 35 56 67 789 21 4 9 34 66
(Atencao)Substitua a Posicao do Vetor entre 0 a 10: 1
Insira um Novo numero no Vetor: 99
Exibindo o novo Vetor… 12 99 35 56 67 789 21 4 9 34 66[/b]