Clément Béra | 2910537 | 2022-09-22 13:10:50 +0200 | [diff] [blame] | 1 | // Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package collectiontoarray; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.Arrays; |
| 9 | import java.util.List; |
Clément Béra | aecefb0 | 2023-01-19 14:51:06 +0100 | [diff] [blame] | 10 | import java.util.function.IntFunction; |
Clément Béra | 2910537 | 2022-09-22 13:10:50 +0200 | [diff] [blame] | 11 | |
| 12 | public class Main { |
| 13 | public static void main(String[] args) { |
| 14 | List<String> list = new ArrayList<>(); |
| 15 | list.add("one"); |
| 16 | list.add("two"); |
| 17 | // This default method was added in Android T. |
| 18 | String[] toArray = list.toArray(String[]::new); |
| 19 | System.out.println(Arrays.toString(toArray)); |
Clément Béra | aecefb0 | 2023-01-19 14:51:06 +0100 | [diff] [blame] | 20 | |
| 21 | List<String> myList = new MyList<>(); |
| 22 | myList.add("one"); |
| 23 | myList.add("two"); |
| 24 | // This default method was added in Android T. |
| 25 | String[] toArray2 = myList.toArray(String[]::new); |
| 26 | System.out.println(Arrays.toString(toArray2)); |
| 27 | } |
| 28 | |
| 29 | @SuppressWarnings("all") |
| 30 | public static class MyList<T> extends ArrayList<T> { |
| 31 | public <T> T[] toArray(IntFunction<T[]> generator) { |
| 32 | System.out.println("Override"); |
| 33 | return super.toArray(generator); |
| 34 | } |
Clément Béra | 2910537 | 2022-09-22 13:10:50 +0200 | [diff] [blame] | 35 | } |
| 36 | } |