I have an array of size 5 that I need to reverse the values based on what is non-empty.
我有一个大小为5的数组,我需要根据非空的内容来反转值。
Where the non-empty values are continuous but they can come in any index: For example I could have 3 non-empty values:
非空值是连续的,但它们可以包含在任何索引中:例如,我可以有3个非空值:
c[0], c[1], c[2] or
c[1], c[2], c[3] or
c[2], c[3], c[4]
or 4 of them coming in any order..
或其中4个以任何顺序出现..
c[0], c[1], c[2], c[3] or
c[1], c[2], c[3], c[4] or
I need to reverse the array only when the values are non-empty. So in case 1 I will have c[2], c[1], c[0] and so on.
我只需要在值为非空时才反转数组。因此,在案例1中,我将有c [2],c [1],c [0]等等。
Case will have c[3], c[2], c[1] and so on..
案例将有c [3],c [2],c [1]等等。
The number of elements that are non-empty and the array is dynamic and generated off a request. I cannot shift the elements to start with index 0 as some outside code relies on the index. All I have to do is reverse this array and send it back.
非空元素的数量和数组是动态的,并根据请求生成。我不能将元素转移到索引0开始,因为一些外部代码依赖于索引。我所要做的就是反转这个数组并将其发回。
I am trying to use a hashmap to mark the indices, and mark the number of non-empty elements in the array. Not sure how to proceed after this, any ideas will be appreciated!
我试图使用hashmap来标记索引,并标记数组中非空元素的数量。在此之后不确定如何继续,任何想法将不胜感激!
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 个解决方案
#1
1
Here's a quick demonstration of a solution that makes use of java.util.Arrays
and java.util.Collections
to do the swapping.
这是一个使用java.util.Arrays和java.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]);
}
}
}
#2
3
You don't need any auxiliary data structures:
您不需要任何辅助数据结构:
-
Find the indices of the first and the last non-empty elements by scanning the array from the start and from the end.
通过从开始和结束扫描数组来查找第一个和最后一个非空元素的索引。
-
Swap the first and the last element.
交换第一个和最后一个元素。
-
Increment the first index, and decrement the last index.
递增第一个索引,并递减最后一个索引。
-
Repeat steps 2 and 3 while
first_index < last_index
.在first_index
时重复步骤2和3。
#1
1
Here's a quick demonstration of a solution that makes use of java.util.Arrays
and java.util.Collections
to do the swapping.
这是一个使用java.util.Arrays和java.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]);
}
}
}
#2
3
You don't need any auxiliary data structures:
您不需要任何辅助数据结构:
-
Find the indices of the first and the last non-empty elements by scanning the array from the start and from the end.
通过从开始和结束扫描数组来查找第一个和最后一个非空元素的索引。
-
Swap the first and the last element.
交换第一个和最后一个元素。
-
Increment the first index, and decrement the last index.
递增第一个索引,并递减最后一个索引。
-
Repeat steps 2 and 3 while
first_index < last_index
.在first_index
时重复步骤2和3。