In Java, I want to delete certain elements from a char array so it does something like:
char[] Array1 = {'h','m','l','e','l','l'};
Array1 = //character index[2] to character index[5]
How can this be done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Java you can’t delete elements from an array. But you can either:
Create a new
char[]copying only the elements you want to keep; for this you could useSystem.arraycopy()or even simplerArrays.copyOfRange(). For example, for copying only the first three characters of an array:Or use a
List<Character>, which allows you to obtain a sublist with a range of elements: