当存在非空的连续元素时,逻辑需要反转数组

时间:2013-03-06 17:52:25

标签: java arrays

我有一个大小为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

只有在值为非空时才需要反转数组。 所以在第一种情况下我会有 c [2],c [1],c [0]等。

案例将有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;    
                  }

2 个答案:

答案 0 :(得分:3)

您不需要任何辅助数据结构:

  1. 通过从开头和结尾扫描数组来查找第一个和最后一个非空元素的索引。

  2. 交换第一个和最后一个元素。

  3. 递增第一个索引,并递减最后一个索引。

  4. first_index < last_index

  5. 时重复步骤2和3

答案 1 :(得分:1)

以下是使用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]);
        }
    }
}