How to copy N characters from char array into another array in Java

1 Answer

0 votes
// public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
 
public class Example {
    public static void main(String[] args) {
              
        char[] formArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        
        int N = 4;
        char[] toArray = new char[N];
        
        System.arraycopy(formArray, 2, toArray, 0, N);
         
        for (char ch : toArray) {
            System.out.println(ch);
        }
    }
}
 
 
 
 
/*
run:
  
c
d
e
f
  
*/

 



answered Jan 15, 2016 by avibootz
edited Aug 15, 2022 by avibootz

Related questions

1 answer 216 views
1 answer 120 views
2 answers 272 views
2 answers 193 views
193 views asked Aug 15, 2022 by avibootz
1 answer 146 views
1 answer 154 views
154 views asked Aug 15, 2022 by avibootz
...