Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to create and initialize a 2d array of characters with different row lengths in Java

1 Answer

0 votes
public class Main {
    public static void main(String[] args) {
         
        char[][] jaggedArray = new char[3][];
         
        // Initialize the rows with different lengths
        jaggedArray[0] = new char[] { 'J', 'a', 'v', 'a' }; 
        jaggedArray[1] = new char[] { 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; 
        jaggedArray[2] = new char[] { 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };

        int rows = jaggedArray.length;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < jaggedArray[i].length; j++) {
                System.out.print(jaggedArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
 
 
/*
run:
 
J a v a 
p r o g r a m m i n g 
l a n g u a g e 
 
*/

 



answered Feb 7, 2025 by avibootz
edited Feb 7, 2025 by avibootz
...