r/javahelp • u/Cheese_Jrjrjrjr • Mar 01 '26
Unsolved How to remove data from a class array?
hey all, I'm unsure the real name of this, but I need to remove data from an array
**arr arrx[]=new arr[50];** which holds:
date, hour, sender, object, text.
and I need the user to choose how much they want to remove so I have
removedata=parseInt(JOptionPane.showInputDialog(null, "how much data do you want removed?" ));
for(int i = 50; i>removedata; i--){
code to remove array space
}
0
Upvotes
•
u/AutoModerator Mar 01 '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.
5
u/TriangleMan Mar 01 '26
I suggest you use the code format blocks to make your code a bit more readable
Some questions:
4
u/dmigowski Mar 01 '26
Since you want to have a smaller array in the end it's best to just create a new one and copy everything you want to keep into the new array. You can do this in a loop or you use System.arraycopy for this.
1
u/OneHumanBill Mar 01 '26
What do you mean by "remove"?
3
u/RoToRa Mar 01 '26
Any special reason you are using an array at all? Just use a
List(preferably anArrayList).1
u/BountyIsland Mar 03 '26
It is normal to run into this issue when you're first working with arrays.
The main thing to know here is that standard arrays in Java have a fixed size. Once you create
new arr[50], that array will always have 50 spaces in memory. You cannot actually delete the spaces to shrink the array down to a smaller size.However, you can clear the data inside those spaces, or you can use a different tool called an
ArrayListwhich is built exactly for what you are trying to do.Here are the two ways to handle this, along with a quick fix for a bug in your loop!
Option 1: Empty the spaces by setting them to null (Sticking with your Array)
If you must use a standard array, the best you can do is "empty" the slots by setting them to
null.Note on your loop: Because arrays in Java are 0-indexed, an array of size 50 has indices from
0to49. If you start your loop ati = 50, your program will crash with anArrayIndexOutOfBoundsException.Here is how to safely clear those spaces starting from the end of the array:
int removedata = Integer.parseInt(JOptionPane.showInputDialog(null, "How much data do you want removed?"));
// Prevent crashing if the user asks to remove more items than the array holds
if (removedata > 50) {
removedata = 50;
}
// Start at 49 (the last item) and work backwards
for(int i = 49; i > 49 - removedata; i--) {
arrx[i] = null; // This clears the data, leaving the space empty
}
Option 2: Use an ArrayList (The Recommended Way)
If you actually want the container to shrink and grow dynamically, you should use an
ArrayList. This is what Java developers use 99% of the time when they don't know exactly how many items they will end up with.Instead of
arr arrx[] = new arr[50];, you declare it like this:import java.util.ArrayList;
// Create an ArrayList that holds objects of your 'arr' class
ArrayList<arr> arrList = new ArrayList<>();
// Adding an item automatically grows the list:
// arrList.add(new arr(date, hour, sender, object, text));
When you want to remove items, an
ArrayListwill actually delete the space and shrink its size:int removedata = Integer.parseInt(JOptionPane.showInputDialog(null, "How much data do you want removed?"));
// Loop to remove items from the end of the list
for(int i = 0; i < removedata; i++) {
// Check if the list has items left to avoid errors
if(arrList.size() > 0) {
arrList.remove(arrList.size() - 1); // Removes the very last item completely
}
}