Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39832
| 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 | 2022-11-03 17:49 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrntm7vog.1qde.avl@logic.at> (permalink) |
| References | <f79712ee-8e36-44e6-97dd-00b271558671n@googlegroups.com> |
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
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