r/javahelp • u/[deleted] • Apr 15 '22
Can split be used on ArrayLists?
I'm doing a coding assignment that requires removing elements from a certain index in an array, so I thought I'd use an arraylist. However, I'm having trouble splitting the arraylist into parts which I can interate through. I tried first using a regular array, splitting it, and then converting it into an arraylist but even that still isn't working. Any advice?
3
u/stayweirdeveryone Apr 15 '22
If you want to remove something from an ArrayList, have you tried using the remove() method?
1
Apr 15 '22
I'm not trying to remove something yet, I'm just trying to split the user after each space and store it in an arraylist
4
u/stayweirdeveryone Apr 15 '22
What does "split the user" mean? Can you post some code of what you're trying to do
2
u/Oceantrader Semijerk Apr 15 '22
If your assignment is asking to use an array, use an array not an array list. It is likely its an exercise in understanding crud operations. Worth reading exactly what it wants. Then make an attempt and post that here and we can help. Arrays do have a copyOfRange but your assignment may want you to iterate and build a new sub array.
Generally speaking an array list is not the best data structure to use when you need to remove items frequently as it is an o(n) operation. It does however work fantastically for get at index as o(1)
1
u/morhp Professional Developer Apr 15 '22
You can use subList() to get a part of a list that you can iterate through.
However I suspect that you have done fundamental understanding error and that this suggestion probably didn't help you. Can you explain what you're trying to do and maybe show some code?
1
u/wildjokers Apr 15 '22
, I'm having trouble splitting the arraylist into parts which I can interate through
What do you mean? Please show some code and more clearly explain what you are trying to do.
•
u/AutoModerator Apr 15 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
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://imgur.com/a/fgoFFis) 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.