r/javahelp • u/[deleted] • Jul 24 '20
Codeless New Java Features implemented by me
I saw a lot of people asking for a lot of features, so I thought, why not implement them? So I created a new project and I added some features a lot of you asked for. I implemented uint, BetterLists (I will talk about them later in this post) and a Testing class.
Basically the better lists are lists that have more methods. They can be reversed and some more features I forgot I added. BUT I added the feature that I saw everyone wanted. When you use the getValue(int index) method, you can pass a negative number. If the number you passed is negative, the list will take it from the end to the beginning. Check below.
BetterList<String> test = new BetterList<String>("1", "2", "3"); test.getValue(2); //will return "3" test.getValue(-2); //will return "2"
So this is what I created. I will give the code soon. Anybody who is interested or wants more features?
1
u/-themailerdoraemon- Jul 24 '20 edited Jul 24 '20
It does not logically and mathematically make sense. :|
the list will take it from the end to the beginning Why change the index sequence to descending from zero to some negative number when one could simple count the indices in reverse?
I mean the index of "1" is logically and mathematically in Java is index 0, so why you would think index 0 is the same as index -2 is beyond me.
1
Jul 24 '20 edited Jul 24 '20
Getting the last item would be -1. Get the item before the last item would be -2. And so on..
1
u/-themailerdoraemon- Jul 24 '20 edited Jul 24 '20
Why are you even changing the sequence of the indices when you could simply reverse them, like literally reverse them, as in 0,1,2,3 and traversing the list in reverse would be 3,2,1,0? You're assigning an index in a List two index #s - one positive # if traversed ascendingly and negative # when traversing descendingly...
Because 0,1,2,3 IS NOT THE SAME as 0, -1, -2, -3
1
Jul 24 '20
Taking the first number is 0. Taking the last number is -1. I just made it so you can get the items in reverse mode. I saw this feature in python iirc.
2
u/-themailerdoraemon- Jul 24 '20
0,1,2,3 is 3,2,1,0 in reverse. Period.
0
Jul 24 '20
0
u/-themailerdoraemon- Jul 24 '20
The reverse of indices 0,1,2,3, n+1 .... is n+1,3,2,1.0
2
u/tiiv Jul 24 '20
Why are you giving him such a hard time? It's probably a concept he picked up in python. Obviously negative indices have no bearing on the layout of the underlying array data structure. You're simply saying if I need to get the last item I use
-1as a placeholder index. If I want to get the second last item I use-2. It's the same as using list.size() - 1 and list.size() - 2 as the index value respectively.2
u/tiiv Jul 24 '20
> Why are you even changing the sequence of the indices when you could simply reverse them
Because it's a much more expensive operation. That's why.
0
u/Satarash Jul 24 '20
It's a cool project even though these two changes are not revolutionary. There are already popular libraries like Guava and Apache Commons which improve collection handling. I'm not familiar with any that have negative indices, but that's a one-liner which is easy to implement, so it's not a feature which would make me add a new dependency to a project.
Does your method reverse mutate the list or returns a copy?
You can edit the original post and fix your example.
1
1
Jul 24 '20
Also, what would make you want this project? I mean, is there anything special you're interested in? What would you like me to add?
0
Jul 24 '20
[deleted]
0
Jul 24 '20
Not really. Getting the last item would be -1. Get the item before the last item would be -2. And so on..
1
u/-themailerdoraemon- Jul 24 '20
It does not logically and mathematically make sense. Do you really think a list, any list, in either real-world or coding scenarios, end with negative number?
1
Jul 24 '20
That isn't the point. I doesn't end with a negative number. Here: Quora thread
0
u/-themailerdoraemon- Jul 24 '20 edited Jul 24 '20
According to your example in your post, index (-2) is value "1". Meaning
Original list: 1 (index 0), 2 (index 1), 3 (index 2)
When looking for value in index -2....
Values: 3 (index 0), value 2 (index -1), value 1 (index -2) ---> this literally ends in a negative number...
Why are you making this so hard on you? A list index begins with 0 and increase by 1. Given the list [1,2,3] -> The index of value "3" is 2. When you reverse the list [1,2,3] to [3,2,1], the index of value "3" is now 0. If the value is in the list, the index is never going to be a negative #.
1
2
u/[deleted] Jul 24 '20
For everyone that needs explanation. I made a mistake in the post.
test.getValue(-2) will return 2.