chitika

Tuesday, September 13, 2011

Java: how to read and write array?

This site provides users with the information about java, read array, write array, how to read array, how to write array, get data from array, set data to array, how to get data from array, how to set data to array, multi-dimensional arrays, and more.

If you think that this site is helpful, please recommend your friends to visit our site.


Java: how to read and write array?

The following is the simple example to read and write array in java programming:

1. Basic array in Java


--------------------------------------------------------------------------------
import java.util.*;

public class array{
  public static void main(String[] args){

  int[] t_array = new int[10];
  
  //Write data to array
  for (int i = 0; i < t_array.length; i++)
  {
 t_array[i] = i;
  }

  int sum = 0;

  //Read data from array
  for (int i = 0; i < t_array.length; i++)
  {
 sum += t_array[i];
  }

  System.out.println(sum);
}
--------------------------------------------------------------------------------

2. Multi-dimensional arrays in Java

--------------------------------------------------------------------------------
import java.util.*;

public class array{
  public static void main(String[] args){

  int[][] m_array = new int[8][6];

  for (int i=0; i < m_array.length; i++) 
  {
     for (int j=0; j < m_array[i].length; j++) 
     {
         System.out.print(" " + m_array[i][j]);
     }
     System.out.println("");
  }
}

No comments:

Post a Comment

For other hundreds of computer repair tips, tricks, tweaks, guide, help, please go to


http://website.edusoftmax.com