0

How to remove String element in array using different method. Any ideas on how to make it? I currently have this but it's not working and I'm out of ideas.

else if(operations ==2){
                String searchStudentNumber = sc.next();
                StudentAssistant[] copy = new StudentAssistant[studAssistant.length-1];
            for(int i=0,j=0;i<studAssistant.length;i++){
                if(copy[i]!= searchStudentNumber){
                    copy[j++] =
                }
            }


//                for(int i = 0; i<sACounter; i++)
////                    if(studAssistant[i].getStudentNumber().equals(searchStudentNumber)){
//                    if(studAssistant[i].equals(searchStudentNumber)){  
////                        studAssistant[i].searchStudentNumber(searchStudentNumber);
//                        studAssistant[i] = null;
//                        break;
//                    }
                  }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    What do you want to remove, what does "remove" mean in your context, is the entry supposed to be empty now: set it to `null` - if you want the next element to take its place, you need to copy each subsequent element one forward, or use a dynamically sized list instead of a raw array. – luk2302 Jan 06 '21 at 15:26
  • i want to remove element from array ex: 1, 2, 3, 4, 5, I want to remove index 0 should be 2,3,4,5 left on array – noobprogrammer Jan 06 '21 at 16:03
  • 1
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Dharman Jan 06 '21 at 16:30

6 Answers6

0

Try to use code below

for(int i=0,j=0;i<studAssistant.length;i++){
    if(!studAssistant[i].equals(searchStudentNumber)){
        copy[j++] = studAssistant[i];
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
white_man
  • 26
  • 1
0

Do you need to use an Array? You can use List of String. One other possible solution could be to take the array and use 'Arrays.asList(yourArray)'. This will return a list. You can then remove the string you want to and then convert it back to an Array.

String stringArray[] = {"1","2","3"};
List<String> list = Arrays.asList(stringArray);
list.remove("2");
Object[] stringRemovedArray = list.toArray();
0

You should use a List instead if you want to delete elements.

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5"));
list.remove(0);//delete element at index 0
list.remove("3");//remove element with value "3"
System.out.println(list);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

you can surely try to use Arrayutils lib from apache .

 ArrayUtils.remove([1], 0)          = []
 ArrayUtils.remove([1, 2], 0)       = [2]
 ArrayUtils.remove([1, 2], 1)       = [1]
 ArrayUtils.remove([1, 2, 3], 1)    = [1, 3]

maven dependency for apache commons will be

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>
Bhupi
  • 387
  • 3
  • 16
0

Using only arrays you could create one more with the same size and mark removed or free_noValue on second.(false)

public class ArrayTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayTest test = new ArrayTest();
    MyArray myArr  = test.new MyArray(3);
    myArr.add(1, 0);
    myArr.add(2, 1);
    myArr.add(3, 2);
    System.out.println(myArr);
    myArr.remove(1);
    System.out.println(myArr);
}

class MyArray
{
    int arr[];
    boolean abool[];
    MyArray(int size)
    {
        arr=new int[size];
        abool=new boolean[size];
        
    }
    public void add(int i,int pos)
    {
            //just replace any value if exists and mark as used
            arr[pos]=i;
            abool[pos]=true;
    }
    
    public void remove(int index)
    {
        abool[index]=false;
    }
    
    public String toString()
    {
        StringBuffer sb= new StringBuffer();
        for(int i=0;i<arr.length;i++)
        {
            if(abool[i])
            {
                sb.append("arr["+i+"]="+arr[i]+"\n");
            }
        }
        return sb.toString();
    }       
}
}

Output

arr[0]=1
arr[1]=2
arr[2]=3
//after remove 1_index
arr[0]=1
arr[2]=3

Basically all the values are on original array but show only the values where flag is true

Traian GEICU
  • 1,750
  • 3
  • 14
  • 26
0

In Java array is immutable, i.e. initialized array cannot change it's size. So to remove one element from the array, you have to create new one with size - 1 and copy all rest items to it.

String[] original = { "0", "1", "2", "3" }; // [ "0", "1", "2", "3" ]
String[] res = removeElement(original, 1);  // [ "0", "2", "3" ]

public static String[] removeElement(String[] arr, int i) {
    assert arr != null;
    assert arr.length > 0;
    assert i >= 0 && i < arr.length;

    String[] res = new String[arr.length - 1];
    System.arraycopy(arr, 0, res, 0, i);
    System.arraycopy(arr, i + 1, res, i, arr.length - i - 1);

    return res;
}

As alternative, you can do it using Apache ArrayUtils:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
String[] original = { "0", "1", "2", "3" };     // [ "0", "1", "2", "3" ]
String[] res = ArrayUtils.remove(original, 1);  // [ "0", "2", "3" ]

Fortunately, there is ArrayList (along with other data types), that does all this internally:

List<String> original = Arrays.asList("0", "1", "2", "3");  // [ "0", "1", "2", "3" ]
original.remove(1); // [ "0", "2", "3" ]
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35