java - 将一个 int 和 string 数组合并到第三个数组中

标签 java arrays merge

There are 2 arrays, one of int and one of Strings (words) ; sort both of them and then print it in a way that at odd places it should be words and at even numbers.

这是我的代码:

import java.util.*;
public class JavaApplication4 {


    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int num[]=new int[10];
        String str[]=new String[10];
        String str1[]=new String[20];
        for(int i=0; i<5; i++)//for taking strings
        {
            str[i]=in.next();
        }
        for(int i=0; i<5; i++)//for taking nums
        {
            num[i]=in.nextInt();
        }
        for(int i=0; i<5; i++)
        {
            System.out.println("The String are "+str[i]);
        }
        for(int i=0; i<5; i++)
        {
            System.out.println("The num are "+num[i]);
        }
         for (int i = 0; i < 5; i++) //for sorting nums
        {
            for (int j = i + 1; j < 5; j++) 
            {
                if (num[i]>(num[j])) 
                {
                    int temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        for (int i = 0; i < 5; i++)// for sorting strs
        {
            for (int j = i + 1; j < 5; j++) 
            {
                if (str[i].compareTo(str[j])>0) 
                {
                    String temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
        System.out.println("The sorted strings are:");
        for(int i=0; i<10; i++)//for merging both
        {
            if((i+1)%2==0)
            {
                int k=0;
                str1[i]=String.valueOf(num[k]);
                 System.out.println("The String are "+str1[i]);
                k++;
            }
            else
            {
                int j=0;
                str1[i]=str[j];
                 System.out.println("The String are "+str1[i]);
                j++;
            }


        }
      /*   for(int i=0; i<10; i++)
        {
            System.out.println("The String are "+str1[i]);
        }
        */
    }
}

我得到的输出:

The sorted strings are:
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1

它只取两个数组的第一个元素。

最佳答案

您应该在循环之前将 kj 初始化为 0,而不是在循环内部。

    int k=0;
    int j=0;
    for(int i=0; i<10; i++)//for merging both
    {
        if((i+1)%2==0)
        {
            str1[i]=String.valueOf(num[k]);
             System.out.println("The String are "+str1[i]);
            k++;
        }
        else
        {
            str1[i]=str[j];
             System.out.println("The String are "+str1[i]);
            j++;
        }
    }

关于java - 将一个 int 和 string 数组合并到第三个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993632/

相关文章:

java - Xamarin KeyStore 密码保护字段

java - 在 Java 中使用 For 循环对通用数组类型列表进行排序

javascript - 更新 Javascript 对象、嵌套数据 - 仅在不可更新时插入

C++ 读取数学函数和排序

php - 合并来自两个不同 MySql 查询的两个不同数组

java - 在 Java 关键部分中,我应该同步什么?

c++ - strncpy导致段错误c++

linux - 按一个字段合并行

svn - SVN对mergeinfo的实际用途是什么?

java - 登录java应用程序