Arrays of Arrays

Question:
Is it possible to have an array of character arrays? What about anarray of multidimensional character arrays?

Answer:
Java arrays are first-class objects. They can be referenced byObject reference variables, and unlike C/C++, do not correspond to rawchunks of memory. When you create an array, its contents are notspecified unless the array is of a native type. The contents of the array are null object references (thereis a short-hand initializer syntax, but that’s just a syntacticconvenience). You must explicitly initialize the elements of thearray.

The types of these elements will depend on the type ofthe array you declared. If you want to store any arbitrary classinstance in the array, including other arrays, you may declare thearray to be of type Object[]. But if you want to limit the type of array to store, say, arrays of characters, you would declare itto be of type char[][]. For example:

    char[][] bar;    bar = new char[50][];    for(int i=0; i < bar.length; ++i) {      bar[i] = new char[5];      for(int j=0; j < bar[i].length; ++j)        bar[i][j] = 'a';    }

You can extend this to store multidimensional character arrays inan array.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.