Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39832

Re: how do you determine whether to start at 0 or 1 ?

Path csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Andreas Leitgeb <avl@logic.at>
Newsgroups comp.lang.java.programmer
Subject Re: how do you determine whether to start at 0 or 1 ?
Date Thu, 3 Nov 2022 17:49:04 -0000 (UTC)
Organization A noiseless patient Spider
Lines 27
Message-ID <slrntm7vog.1qde.avl@logic.at> (permalink)
References <f79712ee-8e36-44e6-97dd-00b271558671n@googlegroups.com>
Reply-To avl@logic.at
Injection-Date Thu, 3 Nov 2022 17:49:04 -0000 (UTC)
Injection-Info reader01.eternal-september.org; posting-host="824b9e953c48755bbdc4a584a2bda47e"; logging-data="1597125"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/obtgSRF/zQ8UBpeXEU++T"
User-Agent slrn/1.0.3 (Linux)
Cancel-Lock sha1:2JFb3afXQbf0BoeaA2GIexJFpZQ=
Xref csiph.com comp.lang.java.programmer:39832

Show key headers only | View raw


karen goh <contact@hi5tutors.com> wrote:
> Hi,
> I have problem in determining to start at 0 or 1 when doing LeetCode DSA questions.
> And also whether to use arrays.length -1 that kind of things as well as using 2 loops
> to do the sorting and whether to start the 2nd loop at 0 or 1 and sometimes people
> use the second loop to start at the first loop 0 index.

Java starts indexing with 0, so for an array like:
   int arr[] = new int[1];
you'd use index 0 for the one and only element, and arr.length would be 1.

A typical loop with an index would (in Java) look like:
   for (int i=0; i<arr.length; i++) {
      ... do something with arr[i]...;
   }
although there's also:
   for (int a : arr) {
      ... do something with a...;
   }
if you only need the values, but not their respective index.

I have no idea what you mean by "LeetCode DSA", and if they have
anything to do with Java, so I don't know, what index they feel like
having for the first element of their arrays.

> Hope someone can give me some guidance.
> Tks.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how do you determine whether to start at 0 or 1 ? karen goh <contact@hi5tutors.com> - 2022-10-20 09:35 -0700
  Re: how do you determine whether to start at 0 or 1 ? Andreas Leitgeb <avl@logic.at> - 2022-11-03 17:49 +0000
    Re: how do you determine whether to start at 0 or 1 ? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2022-11-03 12:52 -0700

csiph-web