我有一个大小为5的数组,我需要根据非空值反转这些值。

非空值是连续的,但它们可以出现在任何索引中:
例如,我可以有3个非空值:

c[0], c[1], c[2] or
c[1], c[2], c[3] or
c[2], c[3], c[4]


或其中4个以任何顺序排列。

c[0], c[1], c[2], c[3] or
c[1], c[2], c[3], c[4] or


我仅在值非空时才需要反转数组。
所以在情况1下
c [2],c [1],c [0]等。

Case将具有c [3],c [2],c [1]等。

非空元素的数量,并且数组是动态的并且是根据请求生成的。由于某些外部代码依赖索引,因此我无法将元素从索引0开头。我要做的就是反转此数组并将其发送回去。

我正在尝试使用哈希图来标记索引,并标记数组中非空元素的数量。不确定之后如何进行,任何想法将不胜感激!

     HashMap<Integer, String> myHash = new HashMap<Integer, String>();

        for(int i = c.length-1; i<=0 ; i--)
            {
                    if(StringUtils.isNotBlank(c[i]))
                    {
                        countNonEmpty++;
                        myHash.put(i, c[i]); //We need to mark the index and decrement by the countNonEmpty
                       }
            }


get the first hash element -
Iterator iter   = myHash.keySet().iterator();
 while(iter.hasNext())
             {
                 Integer correctIndex = (Integer) iter.next();

//But all I need is the first element in hashMap to decide how to set the reverse array's index.
       if(myHash.size() - correctIndex < 0 )  //This means it will have to be 0 to index for array
                  {
                     //What is the right index for c[]
                       c[correctIndex - myHash.size() + 1 ] = myHash.get(correctIndex);
                      continue;
                  }
                  else if(myHash.size() - correctIndex == 0)
                  {   //What is the right index for c[]
                       c[correctIndex - myHash.size() + 1 ] = myHash.get(correctIndex);
                      continue;
                  }

最佳答案

这是使用java.util.Arraysjava.util.Collections进行交换的解决方案的快速演示。

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Reverse
{
    static String[] inputArray = new String[5];

    public static void main(String[] args)
    {
        inputArray[2] = "John";
        inputArray[3] = "Sally";
        inputArray[4] = "Fred";

        int startIndex = 0;
        int endIndex = inputArray.length;

        boolean foundStart = false;
        boolean foundEnd = false;
        System.out.println("before sort");
        for (int index = 0; index < inputArray.length; index++)
        {
            System.out.println(inputArray[index]);

            if (!foundStart && inputArray[index] != null)
            {
                startIndex = index;
                foundStart = true;
            }

            if (foundStart && !foundEnd && inputArray[index] == null)
            {
                endIndex = index;
                foundEnd = true;
            }
        }

        System.out.println("\nafter sort");
        List<String> swapList = Arrays.asList(Arrays.copyOfRange(inputArray, startIndex, endIndex));
        Collections.reverse(swapList);
        System.arraycopy(swapList.toArray(new String[swapList.size()]), 0, inputArray, startIndex, swapList.size());

        for (int index = 0; index < inputArray.length; index++)
        {
            System.out.println(inputArray[index]);
        }
    }
}

10-08 08:42