Java Arrays                 

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    
Java 2  Java 3  Supplementary  Events  AWT Project  AWT Project  Language  Swing  Sound/Images

Arrays in Java function in a similar fashion to arrays in C++, but the way you declare and initialize them differ considerably. Any array in Java is treated as an object. When you create an array in Java, it is equivalent to instantiating an array on the heap in C++. Example:

One Dimensional Arrays
Java:   String[] MyArray = new String[5];
C++:   CString * MyArray = new CString[5];

One Dimensional Arrays
Java:   String[][] MyArray = new String[8][8];
C++:   CString * MyArray = new CString[8][8];

Java will automatically size multi-dimensional arrays according to the number of elements. example:

Java:   String[][] MyArray = {   {"Question1", "Answer 1"},  {"Question 2","Answer 2" }   };

Explicit assignment in 2 dimensional array:

MyArray[0][0] = "test";

You can see by the example above that Java is hiding some pointer magic beneath its hood.

 Example 1- One Dimensional Array:

import javax.swing.*;

public
class CreatingAClass
{
      public void main(String[] args)
      {
             String[] MyArray = new String[5];

             for(int x = 0; x < 5; x++)
             {
                   MyArray[x] =
JOptionPane.showInputDialog(null,
                                "Input value " + x + ": ");
             }           

             System.out.println("Here are the values you typed: ");

             for(int x = 0; x < 5; x++)
             {
                 System.out.print("Value " + x + ": " + MyArray[x] + "\n");
             }             

             System.exit(0);

      }
//close main() function
 
}
//close CreatingAClass class
Java 2  Java 3  Supplementary  Events  AWT Project  AWT Project  Language  Swing  Sound/Images


 Example 2 - Two Dimensional Array:

import javax.swing.*;

public
class CreatingAClass
{
      public void main(String[] args)
      {
            
String[][] MyArray = {  
                                      {"Last Name", "Germany"}, 
                                      {"Favorite Color", "Blue"}  
                                  };


             for(int x = 0; x < 5; x++)
             {
                 System.out.print(MyArray[x][0] + " : " + MyArray[x][1] + "\n");
             }             

             System.exit(0);

      }
//close main() function
 
}
//close CreatingAClass class

 Example 3 - Using Arrays:

import java.util.*;
import javax.swing.*;
public class Array1
{ 
       public static int counter = 0; //global
       public static void main(String[] args)
       {
        int NumElements = 0;
        int x = 0;
        String entry= ""; 
        String ChoiceString = "";
        NumElements = Integer.parseInt(JOptionPane.showInputDialog(null,
                      "How many elements shall I create in the array?")); 
        String[] TheChoices = new String[NumElements];
        Arrays.fill(TheChoices, "0123456789");
        TheChoices[x]  = JOptionPane.showInputDialog(null,
         "Enter items to add to the array. Enter \"quit\"  to  quit");     
        while(!TheChoices[x].equals("quit") && x <  TheChoices.length)
        { 
             //Accumulate the choices from the array and add a line
             ChoiceString = ChoiceString +  TheChoices[x] + "\n";
             ++x;
             if(x <  TheChoices.length)
             {    
                  TheChoices[x]  = JOptionPane.showInputDialog(null,
                  "Enter items to add to the array. Enter \"quit\"  to  quit"); 
             }
        }
        entry = JOptionPane.showInputDialog(null,
                "Today's menu is\n" + ChoiceString +
                "Please make a selection"); 
        for(int z = 0; z < TheChoices.length; z++)
        {
            if(TheChoices[z].equals(entry))
            {   
                  JOptionPane.showMessageDialog(null, 
                  "I found the item in the array!");
                  break;      
            }
            else
            {    
               counter = counter + 1;
            }
        } 
        if(counter == TheChoices.length)
        {
                  JOptionPane.showMessageDialog(null, 
                  "That item was not found.");
        }   
        System.exit(0);   
    }//close main()
}//close class

Java 2  Java 3  Supplementary  Events  AWT Project  HOME  AWT Project  Language  Swing  Sound/Images