Total members 11890 |It is currently Fri Apr 19, 2024 7:27 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





An array is a data structure that stores a collection of values of the same type. You access each individual value through an integer index. For example, if a is an array of integers, then a[i] is the ith integer in the array.

You declare an array variable by specifying the array type—which is the element type followed by []—and the array variable name. For example, here is the declaration of an array a of integers:

Code:
int[] a;
 


However, this statement only declares the variable a. It does not yet initialize a with an actual array. You use the new operator to create the array.
Code:

int
[] a = new int[100];
 


This statement sets up an array that can hold 100 integers.

NOTE
You can define an array variable either as

Code:
int[] a; or as int a[]; 


Most Java programmers prefer the former style because it neatly separates the type int[] (integer array) from the variable name.

The array entries are numbered from 0 to 99 (and not 1 to 100). Once the array is created, you can fill the entries in an array, for example, by using a loop:
Code:

int
[] a = new int[100];
for (int i = 0; i < 100; i++)
   a[i] = i;  // fills the array with 0 to 99

 

practical example Parking Times project using arrays:

The purpose of this program is to reinforce array topics covered in class. Specifically, the assignment is to construct the ParkingTimes class that stores that amount of time spent looking for a parking place each trip to campus and calculates some summary information about these times. Use the test program TestParking.java.

Code:
public class TestParking
{

    public static void main(String[] args)
    {
        ParkingTimes frustration = new ParkingTimes();
        frustration.add(12.3);
        frustration.add(35);
        frustration.add(17);
        frustration.add(5);
        frustration.add(10.3);
        System.out.println ("minimum time spent looking for parking: " + frustration.min());
        System.out.println ("maximum time spent looking for parking: " + frustration.max());
        System.out.println ("average time spent looking for parking: " + frustration.ave());
    }

}
 

This class stores and analyzes the times that ppl spend looking for a parking place.
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parkingtimes;

/**
 *
 * @author sam
 */
public class ParkingTimes extends Object{
    
    private double
[] times = new double[1000];
    private int num = 0;

    public ParkingTimes() {
    }

    public void add(double t) {
        times[num] = t;
        num++;
    }

    public double ave() {
        if (num <= 0) {
            return 0.0;
        }
        double avg = 0.0;
        for (int i = 0; i < num; i++) {
            avg += times[i];
        }
        return avg / num;
    }

    public double max() {
        if (num <= 0) {
            return 0.0;
        }
        double max = times[0];
        for (int i = 1; i < num; i++) {
            if (times[i] > max) {
                max = times[i];
            }
        }

        return max;
    }

    public double min() {
        if (num <= 0) {
            return 0.0;
        }
        double min = times[0];
        for (int i = 1; i < num; i++) {
            if (times[i] < min) {
                min = times[i];
            }
        }

        return min;
    }
    public void increaseSize(){
        
        double 
[]newarray= new double[times.length*2];
        for(int i=0;i<times.length;i++)
        {
            newarray[i]=times[i];
        }
        times = null;
        times = newarray.clone();
       // System.err.println(times.length);
    }
}

 




_________________
Please recommend my post if you found it helpful


Author:
Newbie
User avatar Posts: 14
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Arrays in java
 Multidimensional arrays in Java     -  
 creating arrays in java     -  
 How to compare two arrays in java     -  
 Comparing Arrays in java     -  
 Passing arrays as function parameter in java     -  
 Arrays using Pointers     -  
 Concept of arrays     -  
 Arrays in photoshop     -  
 Defining arrays in ASP     -  
 Multidimensional Arrays in JSP     -  



Topic Tags

Java Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com