如何在Java中关联数组?

时间:2014-04-20 20:10:54

标签: java arrays

我正在写一个关于信号处理的程序。信号出现的一个例子如下:

两个数组:具有相应的元素。他们总是拥有相同数量的元素:

String signal[] = {"x",  "y", "y"  "C", "C", "z","C","C","x","C"};
int time[] =      { 2,    5,   1,   4,   7,   8,  2,  6,  4,  3 };

在信号[]中,x,y和z是三个信号的名称。 C告诉他们继续。 在时间[]中,整数表示当前第一次出现信号的时间,在C数字下表示信号持续了一段时间。数学逻辑如下:

the time of x =2,          (=2)
the time of y = 5          (=5)
the time of y = 1+4+7      (=12)
the time of z = 8 +2+6     (=16)
the time of x = 4+3        (=7)

I need the output as {x=2,  y=5,  y=12,  z=16,  x=7}

如何关联数组元素以获取此输出?

3 个答案:

答案 0 :(得分:4)

这正是java拥有类系统的原因。为什么不为每个信号创建一个包含必要信息的信号类,而不是试图让一堆数组彼此保持一致。或者按建议使用地图?

答案 1 :(得分:1)

package com.company;

public class Main {

    public static void main(String[] args) {

        String signal[] = new String[]{"x", "y", "y", "C", "C", "z", "C", "C", "x", "C"};
        int time[] = new int[]{2, 5, 1, 4, 7, 8, 2, 6, 4, 3};

        int index = 0;
        while (index < time.length) {
            int sum = time[index];
            String sig = signal[index];
            while ((index < (signal.length - 1) && signal[index + 1].equals("C"))) {
                sum = sum + time[index + 1];
                index++;
            }
            System.out.println(sig + " " + sum);
            index++;
        }

    }
}

答案 2 :(得分:1)

这是一个非常糟糕的实现,但如果你没有其他选择而不是你提供的格式化输入,这是我能做的最好的:

public static void main(String[] args)
{
    String signal[] = {"x",  "y", "y",  "C", "C", "z","C","C","x","C"};
    int time[] = { 2,    5,   1,   4,   7,   8,  2,  6,  4,  3 };
    int i=0;
    while (i < signal.length)
    {
        switch(signal[i])
        {

            case "x":
            {
                System.out.print("x = ");
                int sum = time[i];
                if(signal[i+1] == "C")
                {
                    i++;
                    while(i < signal.length && signal[i] == "C" )
                    {
                        sum += time[i];
                        i++;
                    }
                }
                else i++;
                System.out.print(sum + " ");

                break;
            }
            case "y":
            {
                System.out.print("y = ");
                int sum = time[i];
                if(signal[i+1] == "C")
                {
                    i++;
                    while(i < signal.length && signal[i] == "C" )
                    {
                        sum += time[i];
                        i++;
                    }
                }
                else i++;
                System.out.print(sum + " ");

                break;
            }
            case "z":
            {
                System.out.print("z = ");
                int sum = time[i];
                if(signal[i+1] == "C")
                {
                    i++;
                    while(i < signal.length && signal[i] == "C" )
                    {
                        sum += time[i];
                        i++;
                    }
                }
                else i++;
                System.out.print(sum + " ");

                break;
            }
        }
    }

}

输出:x = 2 y = 5 y = 12 z = 16 x = 7