[b]Tenho 3 arrays, duas com 10 posiçoes e uma com 20, estou tentando fazer com que a de 20 receba os valores da array 1 e 2 intercalados.
me ajudem[/b]
eis o código.
[code]
package Prova;
import java.util.*;
public class Questao4 {
public static void main(String[] args) {
recebeDados();
}
public static void recebeDados() {
Scanner sc = new Scanner(System.in);
int array1[]=new int[5];
int array2[]=new int[5];
int recebe[]=new int[10];
for(int i=0;i<array1.length;i++){
System.out.println("Digite um numero do array1");
array1[i]=sc.nextInt();
recebe[i]=array1[i];
System.out.println("Digite os numeros do array2");
array2[i]=sc.nextInt();
recebe[i]=array2[i];
System.out.println(recebe);
}
}
}[/code]
Dei uma editada no seu código vê se resolve seu problema.
Do jeito que tu fazia tava sobrescrevendo a mesma posição no array, além de não preencher “recebe” totalmente.
[code]public static void recebeDados() {
Scanner sc = new Scanner(System.in);
int array1[]=new int[5];
int array2[]=new int[5];
int recebe[]=new int[10];
int aux1=0, aux2=0;
for(int i=0;i<recebe.length;i++){
if(i%2==0 && aux1 <array1.length){
System.out.println(“Digite um numero do array1”);
array1[aux1]=sc.nextInt();
recebe[i]=array1[aux1];
aux1++; //Controlar o total de cadastros no array1
}
else if(aux2 <array2.length){
System.out.println(“Digite os numeros do array2”);
array2[aux2]=sc.nextInt();
recebe[i]=array2[aux2];
aux2++; //Controlar o total de cadastros no array2
}
}
System.out.println("\n\n\nArrays Intercalados");
for(int i=0;i<recebe.length;i++){
System.out.println(recebe[i]);
}
} [/code]
[quote=Dantalian]Dei uma editada no seu código vê se resolve seu problema.
Do jeito que tu fazia tava sobrescrevendo a mesma posição no array, além de não preencher “recebe” totalmente.
[code]public static void recebeDados() {
Scanner sc = new Scanner(System.in);
int array1[]=new int[5];
int array2[]=new int[5];
int recebe[]=new int[10];
int aux1=0, aux2=0;
for(int i=0;i<recebe.length;i++){
if(i%2==0 && aux1 <array1.length){
System.out.println(“Digite um numero do array1”);
array1[aux1]=sc.nextInt();
recebe[i]=array1[aux1];
aux1++; //Controlar o total de cadastros no array1
}
else if(aux2 <array2.length){
System.out.println(“Digite os numeros do array2”);
array2[aux2]=sc.nextInt();
recebe[i]=array2[aux2];
aux2++; //Controlar o total de cadastros no array2
}
}
System.out.println("\n\n\nArrays Intercalados");
for(int i=0;i<recebe.length;i++){
System.out.println(recebe[i]);
}
} [/code][/quote]
cara muito abrigado vou estudar o seu código
mas e isso mesmo que eu quero
valeuuu
Outra maneira:
import java.util.Arrays;
import java.util.Scanner;
public class IntercalarArrays
{
Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
new IntercalarArrays().executar();
}
public int ler(String texto, int posicao)
{
do
{
System.out.printf(texto, posicao);
try
{
return Integer.parseInt(in.nextLine());
}
catch (NumberFormatException e)
{
System.out.println("Valor inválido!");
}
}
while (true);
}
private void executar()
{
final int TAMANHO = 5;
int[] array1 = new int[TAMANHO];
int[] array2 = new int[TAMANHO];
Integer[] recebe = new Integer[TAMANHO * 2];
for (int i = 0; i < array1.length; i++)
{
array1[i] = ler("Digite o %dº valor do array 1: ", i + 1);
}
for (int i = 0; i < array2.length; i++)
{
array2[i] = ler("Digite o %dº valor do array 2: ", i + 1);
}
for (int i = 0; i < TAMANHO; i++)
{
recebe[i * 2] = array1[i];
recebe[i * 2 + 1] = array2[i];
}
System.out.println(Arrays.asList(recebe));
}
}
Outro jeito.
[code] public void misturaArray() {
int[] a1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] a2 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] a3 = new int[20];
int aux = 0;
for (int i = 0; i <= a1.length - 1; i++) {
a3[aux] = a1[i];
aux += 1;
a3[aux] = a2[i];
aux += 1;
}
for (int i : a3) {
System.out.println(i);
}
}[/code]
Abraço!