Shorten array length after deleting element in Java

Note: My next homework / assignment, feel free to answer if you want.

I want to remove / remove an element from an array of String (Set) basic, I am not allowed to use Collections..etc.

Now I have this:

void remove(String newValue) { for ( int i = 0; i < setElements.length; i++) { if ( setElements[i] == newValue ) { setElements[i] = ""; } } } 

I do what I want as it removes the element from the array, but it does not shorten the length. Below is the output, it basically removes the item with index number 1.

 D:\javaprojects>java SetsDemo Enter string element to be added A You entered A Set size is: 5 Member elements on index: 0 A Member elements on index: 1 b Member elements on index: 2 hello Member elements on index: 3 world Member elements on index: 4 six Set size is: 5 Member elements on index: 0 A Member elements on index: 1 Member elements on index: 2 hello Member elements on index: 3 world Member elements on index: 4 six 
+4
source share
5 answers

You cannot change the length of an array object after it is created. Here is an excerpt from JLS 10.2. Array Variables :

After creating an array object, its length never changes. To make an array variable a reference to an array of different lengths, a reference to another array must be assigned to the variable.

This means that for this task you will have to select a new array, which is one element smaller than the original, and copy the remaining elements.

If you need to delete an element with index k , and the original array has elements L , then you need to copy the elements (the upper bounds are exceptional):

  • From [0,k) to [0,k) ( k items)
  • From [k+1,L) to [k,L-1) ( Lk-1 elements).
  • A total of L-1 items copied

 static String[] removeAt(int k, String[] arr) { final int L = arr.length; String[] ret = new String[L - 1]; System.arraycopy(arr, 0, ret, 0, k); System.arraycopy(arr, k + 1, ret, k, L - k - 1); return ret; } static void print(String[] arr) { System.out.println(Arrays.toString(arr)); } public static void main(String[] args) { String[] arr = { "a", "b", "c", "d", "e" }; print(arr); // prints "[a, b, c, d, e]" arr = removeAt(0, arr); print(arr); // prints "[b, c, d, e]" arr = removeAt(3, arr); print(arr); // prints "[b, c, d]" arr = removeAt(1, arr); print(arr); // prints "[b, d]" arr = removeAt(0, arr); arr = removeAt(0, arr); print(arr); // prints "[]" } 

Used by System.arraycopy ; You can always write your own if this is not permitted.

 static void arraycopy(String[] src, int from, String[] dst, int to, int L) { for (int i = 0; i < L; i++) { dst[to + i] = src[from + i]; } } 

This is a simplified implementation that does not handle src == dst , but in this case it is enough.

see also


Note for == for String comparisons

In most cases, using == to compare String objects is a mistake. Instead, you should use equals .

 String ha1 = new String("ha"); String ha2 = new String("ha"); System.out.println(ha1 == ha2); // prints "false" System.out.println(ha1.equals(ha2)); // prints "true" 

see also

+6
source

An array size in Java cannot be resized after an array is created. The following links should help you migrate existing elements to a new array :-)

See: System.arraycopy and Array.copyOf (*) .

+3
source

Basically, you need to create a new array, which is as long as the length of the old array is minus 1, and then you need to copy the valid elements from the old to the new array in a loop, and then replace the old array with a new array.

Since this is homework , details are not listed. Feel free to leave comments for further clarification.

+1
source

All that setElements[i] = ""; does is change the value of an element in an array. In fact, it does not remove anything from the array. If you used a collection class and called it remove(i) , you would actually remove this item from the collection. But here you just change your value. However, Java arrays are fixed in size and cannot be resized, so there is no way to remove elements from them. Therefore, the solution is to create a new array with a shorter length than the old one, and then copy all the values ​​that you want to keep in the new one. Thus,

  • Create a new array with the length setElements.length - 1 .

  • Copy all the elements in setElements into the new array, except the one you want to delete. Be careful that the indexes in the two arrays will be disabled by one, not equal, as soon as you reach the index for the element you want to delete.

  • Set setElements to a new array if you want to use the same variable for your array.

+1
source
 void remove(String newValue) { if(setElements.length == 0) return; String [] array = new String[setElements.length-1]; int j = 0; for ( int i = 0; i < setElements.length; i++) { if ( setElements[i] != newValue ) { array[j++] = setElements[i]; } } setElements = array; } 
0
source

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


All Articles