Path: csiph.com!news.redatomik.org!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: How to parse an array? Date: Tue, 20 Jun 2017 16:15:57 -0400 Organization: Aioe.org NNTP Server Lines: 30 Message-ID: References: NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:36685 On 6/20/2017 3:18 PM, Eric Douglas wrote: > I'm writing a method to take 2 arrays and return the subset of the first array. > Is there a more efficient way than creating a new array and iterating? > > ArrayList list1 = new ArrayList(); > list1.add("AA"); > list1.add("CC"); > list1.add("DD"); > list1.add("BB"); > list1.add("EE"); > ArrayList list2 = new ArrayList(); > list2.add(1); > list2.add(3); > ArrayList retval = new ArrayList(); > for (int i : list2) { > retval.add(list1.get(i)); > } > Collections.sort(retval); > System.out.println(retval); The problems must be O(n) in both list sizes. Your code is O(n). And your code is easy to read. I don't see any urgent need to do it differently. Arne