-1

Please see my code below, in the last two lines of my code that is ArrayList<Float> minList = minsMap.get(minC); and minList.add(itemElement);, I am facing issue. In the first line when value of minC is zero that is minC = 0 then in the second line zero is added to the list instead the value of itemElement. In itemElement i have no zero. Please help me figure out what's wrong with this code.

public class absol 
{

    public static void main(String[] args) 
    {

        float  item[] = {7,4,8,5,6,65,45,33,8,6,3,98};

        final float[][] arrayTwoD = {   {3.5f,  4.55f,  2.1f,   1.40f},
                                        {0.5f,  1.55f,  0.90f,  1.59f},
                                        {4.5f,  5.55f,  3.1f,   2.4f},
                                        {1.5f,  2.55f,  0.10f,  0.60f},
                                        {2.5f,  3.55f,  1.10f,  0.40f},
                                        {61.5f, 62.55f, 60.1f,  59.4f},
                                        {41.5f, 42.55f, 40.1f,  39.4f},
                                        {29.5f, 30.55f, 28.1f,  27.4f},
                                        {4.5f,  5.55f,  3.1f,   2.4f},
                                        {2.5f,  3.55f,  1.10f,  0.40f},
                                        {0.5f,  0.55f,  1.90f,  2.6f},
                                        {94.5f, 95.55f, 93.1f,  92.4f}, 
                                    };


        Map<Integer, ArrayList<Float>> minsMap = new LinkedHashMap<>();

        // Initializing Map
        for(int i=0; i<arrayTwoD[0].length; i++)
        {
            ArrayList<Float> minList =new ArrayList<>();
            minList.add(0.0f);
            minsMap.put(i, minList);
        }


        for(int row=0; row < arrayTwoD.length; row++)
        {
            float min = arrayTwoD[row][0];
            int minC = 0;
            float itemElement =0;

            for(int col=0; col < arrayTwoD[row].length; col++)
            {
                if(arrayTwoD[row][col] < min) 
                {       
                    min = arrayTwoD[row][col];

                    itemElement = item[row];
                    minC = col;
                }
            }

            ArrayList<Float> minList = minsMap.get(minC);
            minList.add(itemElement);

        }

//      for(int i= 0; i< minsMap.size(); i++)
//      {
//          System.out.print("\n\t Values in List "+i);
//          for(int j= 0; j< minsMap.get(i).size(); j++)
//          {
//              System.out.print("\t"+minsMap.get(i).get(j));   //////////////////////////////
//          }
//      }
    }

}
4

2 回答 2

2

我不确定您要达到什么目标,但是您得到的零比预期的多的原因是这种情况:

if (arrayTwoD[row][col] < min) {...}

如果第一项 ( float min = arrayTwoD[row][0];) 是最小值,则不执行 if 块并且您已初始化float itemElement = 0;...

于 2013-05-16T23:23:30.673 回答
0

代替

float itemElement =0;

float itemElement = item[minC];

您正在使用 0 进行初始化,而不是正确的项目值。

于 2013-05-16T23:24:05.247 回答