Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb 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: References: 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 karen goh 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 Hope someone can give me some guidance. > Tks.