我可以将数组存储到另一个数组吗?

时间:2016-05-28 03:39:00

标签: java

我正在尝试在java中创建一个程序,我在其中为男孩创建了一个数组,其中包括男孩的名字,一个用于感觉(爱,恨,崇拜),第三个用于包含名字的女孩女孩

现在我为每个数组生成了随机数,并将它们连接起来以提供...(boys[]+" "+feelings[]+" "+girls[])之类的输出,例如Sagar hates Sakshi

现在这句话与男孩有关。我想为Sakshi Love Shubham这样的女孩生成另一个字符串。现在这些新生成的字符串也应该随机打印。我该怎么做?

我的源代码是这样的......

public class Random
{
    public static void main(String args[])
    {
        String[] boys={"shivam","shubham","sagar","Tushar","tarun"}; String[]=girls{"payal","preeti","neetika","sakshi_jain","sakshi_singh","wafa","Reshu","Pragya"};
        String[] feelings={"Love","Hate","Adore","want_to_kill"};

        int x=boys.length;
        int y=girls.length;
        int z=feelings.length;

        for(int i=0;i<10;i++)
        {
            int rand1=(int)(Math.random()*x);
            int rand2=(int)(Math.random()*y);
            int rand3=(int)(Math.random()*z);

            String phase1=girls[rand2]+" "+feelings[rand3]+" "+boys[rand1];
            String phase2=boys[rand1]+" "+feelings[rand3]+" "+girls+[rand2];
            String[][] phase={phase1,phase2};

            int p=phase.length;
            int q=(int)(Math.random()*p);

            System.out.println(phase[0][q]);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请您检查以下解决方案:

public class Random {
    public static void main(String args[]) {
        String[] boys = { "shivam", "shubham", "sagar", "Tushar", "tarun" };
        String[] girls = { "payal", "preeti", "neetika", "sakshi_jain", "sakshi_singh", "wafa", "Reshu", "Pragya" };
        String[] feelings = { "Love", "Hate", "Adore", "want_to_kill" };

        String[][] emotions = new String[2][];
        emotions[0] = new String[boys.length];
        emotions[1] = new String[girls.length];

        // boys emotions
        for (int i = 0; i < boys.length; i++) {
            String subject = boys[i];
            String verb = getRandomItem(feelings);
            String object = getRandomItem(girls);
            emotions[0][i] = subject + " " + verb + " " + object;
        }

        // girls emotions
        for (int i = 0; i < girls.length; i++) {
            String subject = girls[i];
            String verb = getRandomItem(feelings);
            String object = getRandomItem(boys);
            emotions[1][i] = subject + " " + verb + " " + object;
        }

        // printing two dimensional array
        for (int i = 0; i < emotions.length; i++) {
            for (int j = 0; j < emotions[i].length; j++) {
                System.out.println(emotions[i][j]);
            }
        }

    }

    // returns a random element
    private static String getRandomItem(String[] arr) {
        int max = arr.length - 1;
        int min = 0;
        java.util.Random random = new java.util.Random();
        int index = random.nextInt(max - min + 1) + min;
        return arr[index];

    }
}

并告诉我结果?

答案 1 :(得分:0)

您可以声明一个包装类(Phase)来存储String对,并使用ArrayList来存储“Phase class”。

import java.util.ArrayList;

public class Test
{
    public static void main(String[] args) {
        String[] boys={"shivam","shubham","sagar","Tushar","tarun"};  String[]girls={"payal","preeti","neetika","sakshi_jain","sakshi_singh","wafa","Reshu","Pragya"};
        String[] feelings={"Love","Hate","Adore","want_to_kill"};

        int x=boys.length;
        int y=girls.length;
        int z=feelings.length;

        for(int i=0;i<10;i++)
        {
            int rand1=(int)(Math.random()*x);
            int rand2=(int)(Math.random()*y);
            int rand3=(int)(Math.random()*z);

            String phase1=girls[rand2]+" "+feelings[rand3]+" "+boys[rand1];
            String phase2=boys[rand1]+" "+feelings[rand3]+" "+girls[rand2];

            ArrayList<Phase> phase = new ArrayList<>();
            phase.add(new Phase(phase1, phase2));

            int p=phase.size();
            int q=(int)(Math.random()*p);

            System.out.println(phase.get(q));
            System.out.println("**************");
        }
    }
}

class Phase {
    String phase1;
    String phase2;

    public Phase(String p1, String p2) {
        this.phase1 = p1;
        this.phase2 = p2;
    }
    public String toString(){
        return phase1 + "\n" + phase2;
    }
}