#How to read debug

91 messages ยท Page 1 of 1 (latest)

strange oracle
#

I fail to read the debug on this

spice girderBOT
#

โŒ› This post has been reserved for your question.

Hey @strange oracle! Please use /close or the Close Post button 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.

strange oracle
#

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 ?

steady oracle
#

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

steady oracle
#
int maxValue = list.get(0);
int maxIndex = 0;
int minValue = list.get(0);
int minIndex = 0;
....
steady oracle
strange oracle
#

oh I see

#

but isnt that the same speed

#

as getting the index later on

steady oracle
strange oracle
#

oh I see

#

I thought indexOf just had a record of all indexes and theier values whenever u made a new list

steady oracle
steady oracle
strange oracle
#

I didnt view that yet

steady oracle
#

but if List is implementation of linkedList - it stores head node, and it walks through the lsit of nodes

steady oracle
#
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)

strange oracle
#

iterator.hasNext() means there is more elements to go through ?

steady oracle
#

yes

strange oracle
#

ElementType element = iterator.next()

#

we jump to the next element in the list

#

?

steady oracle
#

yes

strange oracle
#

and after ...

#

the instructions to do

#

?

steady oracle
#

yeah, like you compare with max/min or whateven you want to do with element

strange oracle
#

ok

#

and how do u go next one after

#

ElementType element = iterator.next();

#

one of these at the end too

#

?

steady oracle
strange oracle
#

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

steady oracle
#

it executes the while loop

#

if there are more elements in list, list.hastNext() teturns true, and iteration executes again

strange oracle
#

oh

#

as long as iterator has a next value to jump on

#

then jump to it

steady oracle
#

yes

strange oracle
#

as long as it has next value

#

we call ElementType element = iterator.next();

#

and we check for that one

steady oracle
#

yes

strange oracle
#

right ?

#

yeah

#

ok

#

i get it now

#

so

#

for() for arrayList

steady oracle
#

right

strange oracle
#

and while for linkedlist

steady oracle
# strange oracle 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

strange oracle
#

because linked lists cant get instan access to element in list ?

steady oracle
#

yes.

#

because for linkedlist method list.get(index) has to iterate in loop to get i-th element

strange oracle
#

and that takes much more time

#

than using iterator and while

steady oracle
#

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)

strange oracle
#

oh damn

#

I see

#

i get the point now

#

tysm

steady oracle
#

you are welcome ๐Ÿ˜‰