#How to read debug
91 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @strange oracle! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
I dont know hwo to fix the error length
idk why its caused even
list size is 4
it has 4 elements
1,4,2,0
and the minimum is 0
at position 3
so why cant it remove it
idk
is it because the index updates
so like after deleting the max
the min will have a different position
i have to put posmin-1
or
instead of saving values for posmin or posmax
could just put list.indexOf(max) in the remove thing directly
which one is more correct ?
you should delete element with higher index first if you don't want to shift second index
your current approach isn't best. In your previous post suggestion to use indexes was made, but you use it a little in wrong way, imo
among max and min values you should also store their indexes. so you don't need to use list.indexOf
wdym it stores with them
int maxValue = list.get(0);
int maxIndex = 0;
int minValue = list.get(0);
int minIndex = 0;
....
๐
when you use indexOf, you make it iterate through all the elements to find what you need.
handling index manually, you can avoid that
oh I see
I thought indexOf just had a record of all indexes and theier values whenever u made a new list
well, your method accepts List - we don't know that's arraylist or linkedlist. So, imo best would be to iterate using iterator, because list.get(i) makes list to iterate i times from head of list to get i-th element.
it depends of implementation of list. ArrayList stores values in array, so list.get(i) can immediately get the value, it just returns ith element of array (like retrun innerArray[i])
iterator ?
I didnt view that yet
but if List is implementation of linkedList - it stores head node, and it walks through the lsit of nodes
you use if implicitly when you walk elements of list using foreach loop
for (ElementType element : list) { ... }
// equivalent to
Iterator<ElementType> iterator = list.iterator();
while (iterator.hasNext()) {
ElementType element = iterator.next();
...
}
it just moves cursor over the list (that is very useful if you work with linkedlist)
iterator.hasNext() means there is more elements to go through ?
yes
ElementType element = iterator.next()
we jump to the next element in the list
?
yes
yeah, like you compare with max/min or whateven you want to do with element
ok
and how do u go next one after
ElementType element = iterator.next();
one of these at the end too
?
what?
int nblargestrings = 0:
Iterator<String> iterator = list.iterator()
while(iterator.hasNext()){
String element = iterator.Next();
if(element.length>10){
nblargestrings++;
{
{
how do u jump to the next string
to check if its larger than 10 or no
it executes the while loop
if there are more elements in list, list.hastNext() teturns true, and iteration executes again
yes
as long as it has next value
we call ElementType element = iterator.next();
and we check for that one
yes
it returns true and the thing to go to the next value is ElementType element = iterator.next();
right ?
yeah
ok
i get it now
so
for() for arrayList
right
and while for linkedlist
for (int i = 0; ...) is ok for arraylist, since it can get instant access to element in array by it's index
foreach or iterator+while - are same, and useful for linked list
because linked lists cant get instan access to element in list ?
yes.
because for linkedlist method list.get(index) has to iterate in loop to get i-th element
yes. if you do something like java for (int i = 0; i < lise.size(); i++) { Elementtype item = list.get(i); ... }
Time complexity of walking through whole list is O(n^2)
but it you use iterator approach (foreach loop or iterator+while) it works for O(n)
you are welcome ๐