| |

VerySource

 Forgot password?
 Register
Search
View: 822|Reply: 4

Collections.copy problems. . .

[Copy link]

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-4 01:20:01
| Show all posts |Read mode
import java.util. *;

public class Copy
{
public static void main (String [] args)
{
List source = Arrays.asList ("three Two Yo Yo six five Four" .split (""));
List dest = Arrays.asList ("in the matrix" .split ("m"));
Collections.copy (dest, source);
System.out.println (dest);
}
}
Don't know why an exception is thrown here?
Reply

Use magic Report

0

Threads

12

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-4 06:06:01
| Show all posts
Collections jdk source code

public static <T> void copy (List <? super T> dest, List <? extends T> src) {
        int srcSize = src.size (); // source array
        if (srcSize> dest.size ())
            throw new IndexOutOfBoundsException ("Source does not fit in dest");
        // If the size () of the source array> size () of the destination array, then IndexOutOfBoundsException is thrown
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-4 07:54:01
| Show all posts
Collections.copy (dest, source);
Insufficient space or type mismatch will throw an exception
Reply

Use magic Report

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-1-4 09:54:01
| Show all posts
knock off
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Japan

Post time: 2020-9-3 22:15:01
| Show all posts
The length of the target array is 0 before initialization, you want to display the set target array length> source array length, and then copy.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list