Java Tutorial with Java Code Examples

Java Basics | String | Date | Collection | Java Map | Java Util | File I/O | XML | Threading | Network | i18n | Misc
Java Swing | JDBC | Servlet | JSP | JSTL | JMS | EJB | JPA | JSF | Struts | Spring | Hibernate
Web Services | Java PDF | JUnit | Ant | HTML | JavaScript | Apache | Tomcat | SQL | T-SQL | PL/SQL | MySQL

Java Basics

Introduction

Data Types and Arrays

Get User Input

Data Type Conversions

Control Flow

Handling Exception

 

  Java Basics » Data Types and Arrays » Arrays

  Java Code Example:  ArrayExample.java  Download Sample Source Code

/*
 * Copyright © 2008 - 2009 it.ibluespace.com. All rights reserved.
 */
public class ArrayExample { public static void main(String args[]) { //To create and initialize an array of 3 String objects. String [] strArray = {"Red", "Green", "Blue"}; //To create an array of 4 integers. int [] intArray = new int [4]; //An array is accessed by its index. //An array index is an Integer ranging from 0 to the array length - 1 . intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; intArray[3] = 4; //The length of an array is the element number in the array. //To get the length of the array. int strArrayLength = strArray.length; System.out.println("The length of strArray is: " + strArrayLength); System.out.println("strArray[0] = " + strArray[0]); System.out.println("strArray[1] = " + strArray[1]); System.out.println("strArray[2] = " + strArray[2]); System.out.println(); //To get the length of the array. int intArrayLength = intArray.length; System.out.println("The length of intArray is: " + intArrayLength); System.out.println("intArray[0] = " + intArray[0]); System.out.println("intArray[1] = " + intArray[1]); System.out.println("intArray[2] = " + intArray[2]); System.out.println("intArray[3] = " + intArray[3]); System.out.println(); //Array of Array int [][] matrix = { { 1, 2, 3 }, { 4, 5, 6} }; //matrix[0] is actually an array with value { 1, 2, 3 }; //so the length of matrix[0] is 3 which is the column count of the array System.out.println("matrix[0][0] = " + matrix[0][0]); System.out.println("matrix[0][1] = " + matrix[0][1]); System.out.println("matrix[0][2] = " + matrix[0][2]); //matrix[1] is actually an array with value { 4, 5, 6}; //so the length of matrix[1] is 3 which is the column count of the array. System.out.println("matrix[1][0] = " + matrix[1][0]); System.out.println("matrix[1][1] = " + matrix[1][1]); System.out.println("matrix[1][2] = " + matrix[1][2]); System.out.println(); //To get the row count of array matrix. int matrixRowCount = matrix.length; //To get the row count of array matrix. int matrixColCount = matrix[0].length; System.out.println("matrix row count = " + matrixRowCount); System.out.println("matrix column count = " + matrixColCount); } }
 Download Source Code: ArrayExample.java

The command executed and output produced:


E:\JavaEECoding\>java ArrayExample
The length of strArray is: 3
strArray[0] = Red
strArray[1] = Green
strArray[2] = Blue

The length of intArray is: 4
intArray[0] = 1
intArray[1] = 2
intArray[2] = 3
intArray[3] = 4

matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6

matrix row count = 2
matrix column count = 3

Search for Java code examples on this site

 
 

Resources

Java Tutorial

Java World

Java SE 6 API Doc


Terms of Use  |  License  Copyright 2008-2009 it.ibluespace.com. All rights reserved.  | 
All other trademarks are the property of their respective owners.