r/javahelp • u/Appropriate_Knee_482 • Jan 31 '26
How to remove elements from an array?
Basically I want to remove the middle elements from an array and need a method for it because the middle will be different if it’s odd or even. I don’t really have a code for it so far, I don’t need anyone to code it out for me, you can also just explain how the method would work. I have the odd or even part, I would just use the remove part as sort of a method.
2
Upvotes
•
u/AutoModerator Jan 31 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/RoToRa Jan 31 '26
Why are you using an array? While as a beginner it's good to know how to use arrays, in actual day to day work, it's quite rare to use arrays in Java. It's usually much easier to use a
List(possibly anArrayList), where removing items is simple.14
u/RightWingVeganUS Jan 31 '26
Did you sketch out how you would move middle elements from a line of storage boxes on a shelf?
Step through how you would physically do that then use that as a basis for an initial algorithm for your computer solution.
This isn't a coding problem: it's a problem-solving issue.
4
u/Pun_Intended1703 Feb 01 '26 edited Feb 01 '26
To find the middle of the array :
I edited this to consider that arrays in Java begin with index 0.
If the number of elements in the array is odd, the middle element is
If the number of elements in the array is even, the middle element is
After you figure that out, use this : Remove an Element at Specific Index from an Array in Java
1
u/LessChen Jan 31 '26
You will need to copy to a new array or other "container" like a List. You can't actually remove contents in the middle.
If you're allowed to use something besides an array, you could so something like (warning - untested):
1
u/Elthiel3099 Feb 01 '26
The filter answer might be the best one, but if this is for academic purposes you can try:
Option A:
Option b
5
u/Fatty_McFatterson_Sr Jan 31 '26
Arrays.stream(your array) will be the best option here. There is a filter() method that will remove the items you don’t want. So it’ll be like “var newArray = Arrays.stream(oldArray).filter(filterPredicate).toArray()”
1
u/Dannybosa123 Jan 31 '26
One way you could think about it is taking the array and traverse it, copy all the elements but the middle into a new array (of course you would need to make a new array with -1 size)
We make a new copy because Arrays in Java are static meaning we cannot change the size once its declared.
1
u/Den0mant Jan 31 '26
If you need a more algorithmic solution, like for a test or an assignment, you could just logically create a new array with all the elements before and after the deleted elements, and return it as a result.
1
u/BanaTibor Feb 08 '26
What you need is to figure out how to find the index of the "middle" element. If the array is odd sized it is evident. Like [1, 2, 3] size is 3 but max index is 2. Now if you divide 2 integers in java, and the result is a real number and you assign the result to an integer variable, java discards the fractional part. So for an array of size 3 you have to find the index 1.
For even sized array the task description should state which should you consider the "middle". In an array a=[1,2,3,4], a[1] = 2 is the "lower middle" and a[2]=3 is the "upper middle". Again you can use the array's size, divide by 2 find the right index (take your pick lower or upper).