Copy String array to new String array

what am i doing wrong here? I want to somehow remove empty elements in my String array. This is what I have tried so far:

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]; } } 

no matter what I try, it always goes into copying all the values: S I checked the local objects and it is clear that there are empty arrays in the String array

+4
source share
5 answers

You copy an array of the same length and use the same indexes. The length will always be the same.

 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()] ); 
+7
source

Try it,

 b = Arrays.copyOf(a, a.length); 

or

 b = new int[a.length]; System.arraycopy(a, 0, b, 0, b.length); 

or

 b = a.clone(); 
+9
source
 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); } 

NOTE: This may not be an effective solution, but it will produce a result according to your requirement.

0
source

This is just an alternative solution since you did not want ListArray . Read the comments in the code to clearly understand the logic.

 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++; } 

That should do the job. Also, I would suggest not working with the 0th position of the array, as it creates confusion for the .length functions. This is just my opinion. If you like it, go on !: D

0
source

If you are looking for a high performance solution, I think this is the best solution. Otherwise, if your input array is not so huge, I would use a solution similar to Peter Lawrey, so your code is easy to understand.

With this solution, you only loop the input array, and if you do not need the input array, you can avoid one instance of the copy instance that filterBlankLines called with preserveInput = false.

 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+"<"); } } 

}

Exit:

 Before: length 8 >< >1< >< >null< >2< > < >3< >< After: length 3 >1< >2< >3< 
0
source

Source: https://habr.com/ru/post/1403301/


All Articles