将字符串数组复制到一个新的字符串数组中
我在这里做错了什么?我想以某种方式消除我的字符串数组中的空元素。这是我到目前为止已经试过:将字符串数组复制到一个新的字符串数组中
String version = null;
String[] xml = new String[str.length];
for(int i = 0; i <= str.length -1; i++)
{
if(str[i] == "")
{
}
else
{
xml[i] = str[i];
}
}
String version = null;
String[] xml = new String[str.length];
for(int i = 0; i <= str.length -1; i++)
{
if(!str[i].equals(""))
{
xml[i] = str[i];
}
}
String version = null;
String[] xml = new String[str.length];
for(int i = 0; i <= str.length -1; i++)
{
if(!str[i].isEmpty())
{
xml[i] = str[i];
}
}
String version = null;
String[] xml = new String[str.length];
for(int i = 0; i <= str.length -1; i++)
{
if(str[i].isEmpty() == false)
{
xml[i] = str[i];
}
}
不管我是哪一个,它总是进入复制所有的值:S I已经检查了当地人,很明显,有内空数组字符串数组
您正在复制相同长度的数组并使用相同的索引。长度总是一样的。
List<String> nonBlank = new ArrayList<String>();
for(String s: str) {
if (!s.trim().isEmpty()) {
nonBlank.add(s);
}
}
// nonBlank will have all the elements which contain some characters.
String[] strArr = (String[]) nonBlank.toArray(new String[nonBlank.size()]);
String str[] = {"Hello","Hi","","","Happy","","Hmm"};
int count = 0;// Thisreprents the number of empty strings in the array str
String[] xml = new String[str.length];
for(int i = 0,j=0; i <= str.length -1; i++)
{
if(str[i].equals(""))
{
count++;
}
else
{
xml[j] = str[i];j++;
}
}
String a[] = Arrays.copyOfRange(xml, 0, xml.length-count);//s is the target array made by copieng the non-null values of xml
for(String s:a){
System.out.println(s);
}
注:这可能不是一个有效的解决方案,但它会给结果按照您的要求
这仅仅是一个替代的解决方案,因为你不想ListArray
。 阅读代码中的注释以清楚地理解逻辑。
int i,j=0,cnt=0;
//The below for loop is used to calculate the length of the xml array which
//shouldn't have any empty strings.
for(i=0;i<str.length;i++)
if(!isEmpty(str[i])
cnt++;
//Creation of the xml array with proper size(cnt) and initialising i=0 for further use
String xml[]=new String[cnt];
i=0;
//Simply copying into the xml array if str[i] is not empty.Notice xml[j] not xml[i]
while(i<str.length)
{
if(!isEmpty(str[i]))
{
xml[j]=str[i];
i++;
j++;
}
else
i++;
}
这应该做的工作。 此外,我会建议不与阵列的第0个位置一起工作,因为它会造成.length
函数的混淆。这仅仅是我的观点。如果你对此感到满意,继续! :D
如果您正在寻找高性能解决方案,我认为这是最好的解决方案。否则,如果你的输入数组不是那么大,我会使用类似于Peter Lawrey的解决方案,所以它使你的代码易于理解。
使用此解决方案,您只能将输入数组循环一次,并且如果不再需要输入数组,则可以避免一次使用preserveInput = false调用filterBlankLines的数组副本。
public class CopyingStringArrayIntoNewStringArray {
public static void main(String[] args) {
String[] str = { "", "1", "", null, "2", " ", "3", "" };
System.out.println("\nBefore:");
printArrays(str);
String[] xml = filterBlankLines(str, true);
System.out.println("\nAfter:");
printArrays(xml);
}
private static String[] filterBlankLines(String input[], boolean preserveInput) {
String[] str;
if (preserveInput) {
str = new String[input.length];
System.arraycopy(input, 0, str, 0, input.length);
} else {
str = input;
}
// Filter values null, empty or with blank spaces
int p=0, i=0;
for (; i < str.length; i++, p++) {
str[p] = str[i];
if (str[i] == null || str[i].isEmpty() || (str[i].startsWith(" ") && str[i].trim().isEmpty())) p--;
}
// Resize the array
String[] tmp = new String[ p ];
System.arraycopy(str, 0, tmp, 0, p);
str = null;
return tmp;
}
private static void printArrays(String str[]) {
System.out.println("length " + str.length);
for (String s : str) {
System.out.println(">"+s+"<");
}
}
}
输出:
Before:
length 8
><
>1<
><
>null<
>2<
> <
>3<
><
After:
length 3
>1<
>2<
>3<
试试这个,
b = Arrays.copyOf(a, a.length);
或者
b = new int[a.length];
System.arraycopy(a, 0, b, 0, b.length);
或
b = a.clone();
它帮助我很多 – NovusMobile 2016-05-31 06:59:32
虽然这段代码可能有助于解决问题,但它并不能解释_why_和/或_how_它是如何回答问题的。提供这种附加背景将显着提高其长期教育价值。请[编辑]您的答案以添加解释,包括适用的限制和假设。 – 2016-08-26 07:50:39
你确定它们没有空白吗? – cHao 2012-03-25 08:41:41
顺便说一句,你会意识到,只要你不复制一个字符串,你就会在'xml [i]'处留下'null',对吧?对两个数组使用相同索引的后果之一... – cHao 2012-03-25 08:43:17
您确定它们是空的(而不仅仅是仅包含空白字符的字符串)吗?也许在执行比较之前尝试修剪('str [i] .trim()')?请注意,'=='不是你想要的,但其他两个都应该工作。 – 2012-03-25 08:43:32