Preview

Example of Array for It Students

Better Essays
Open Document
Open Document
1094 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Example of Array for It Students
Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.

class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray;

// allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // etc. anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000;

System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]); System.out.println("Element at index 2: " + anArray[2]); System.out.println("Element at index 3: " + anArray[3]); System.out.println("Element at index 4: " + anArray[4]); System.out.println("Element at index 5: " + anArray[5]); System.out.println("Element at index 6: " + anArray[6]); System.out.println("Element at

You May Also Find These Documents Helpful

  • Good Essays

    Nt1420 Unit 6

    • 1145 Words
    • 5 Pages

    Question 6 A hash table is a data structure that uses an array as its internal storage container. Items are added to the array based on the integer generated by a hash function. A hash function produces an integer based on some properties of the object. In Java hash functions are encapsulated via the hashcode method in the Object class and that many classes override.…

    • 1145 Words
    • 5 Pages
    Good Essays
  • Powerful Essays

    System.out.println("i = " + i + " f = " + f + " d = " + d);…

    • 2338 Words
    • 10 Pages
    Powerful Essays
  • Good Essays

    A set is a collection of objects. The objects in a set are called the elements of the set. A set of numbers is simply a listing, within braces {}. For example, the set of numbers used for counting can be represented as S = {1, 2, 3, 4, 5, . . .}. The braces…

    • 1254 Words
    • 6 Pages
    Good Essays
  • Good Essays

    Arrays store items that have the same type of data type like a group of employees’ names and social security numbers for a team of 2000 personal. Pointer is a variable that greatly extends the power and flexibility of a program, each memory location that is used to store data value has an address. The address provides the means for a PC hardware to reference a particular data item.…

    • 485 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Array Structure Paper

    • 833 Words
    • 4 Pages

    “An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all characters, and so on” (Thompson, 2007). Building an array can be confusing for the Bug Blasters can be very confusing for a programmer who does not fully understand how they work.…

    • 833 Words
    • 4 Pages
    Good Essays
  • Good Essays

    Sheet Java Study Guide

    • 576 Words
    • 3 Pages

    1. Write a program to compute the area and circumference of a rectangle 3 inche wide by 5 inches long.…

    • 576 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    E4.6: The label array_x is the starting address of an array of 100 8bit elements.…

    • 900 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    array - noun an arrangement of aerials spaced to give desired directional characteristics; especially fine or decorative clothing; an impressive display; an orderly arrangement; verb align oneself with a group or a way of thinking; lay out orderly or logically in a line or as if in a line…

    • 845 Words
    • 4 Pages
    Good Essays
  • Good Essays

    Programming Homework

    • 366 Words
    • 2 Pages

    The output required from this program will be a multiplication table going from 1 x 1 all the way to 10 x 10 with a new line after every 10 products.…

    • 366 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Programming Logic

    • 1499 Words
    • 6 Pages

    2. Design the logic for a program that outputs every number from 1 through 10.…

    • 1499 Words
    • 6 Pages
    Good Essays
  • Good Essays

    • You have approximately 25 hours in which to complete this scenario. • Before starting work on the problem, read the whole of this Candidate Booklet thoroughly. You can…

    • 1453 Words
    • 6 Pages
    Good Essays
  • Good Essays

    Assignment 3 CS 113

    • 804 Words
    • 5 Pages

    For each number x input by the user the program displays (prints) “x is in the list” or…

    • 804 Words
    • 5 Pages
    Good Essays
  • Good Essays

    Lab_6_solution

    • 1259 Words
    • 3 Pages

    By the end of this lab session, you will be able to - declare an integer and character array - assign values to array during compilation and during execution - print contents of an array using repetition structure SAMPLE PROGRAM 1 /1. A program reads a series of numbers and print it reversed / include stdio.h void main (void) int i, Num, numbers10 printf(You may enter up to 10 integersn) printf(How many integers would you like to enter ) scanf (d, Num) if (Num 10) Num 10 printf(nEnter your numbers ) for (i 0 i Num i) scanf(d, numbersi) printf(nYour numbers reversed are ) for (i Num - 1 i 0 --i) printf(d , numbersi) printf(nn) SAMPLE PROGRAM 2 /2. Program that demonstrates the use of for loop to accept 8 numbers from user and displays certain numbers from the array / includestdio.h void main() int num, numbers8 printf(You need to enter 8 numbers ... n) for(num 0 num 7 num) scanf(i,numbersnum) printf(nn) printf(Your 1st element is dn,numbers0) printf(Your 2nd element is dn,numbers1) printf(Your 3rd element is dn,numbers2) printf(Your 8th element is dn,numbers7) printf(nn) printf(nnAll together your numbers aren) for(num 0 num 7 num) printf(d ,numbersnum) printf(nn) SAMPLE PROGRAM 3 /3. Program declares an array called charArray and assigns a single character to each of the array element using the initialize braces. Finally, the program prints the content of the array. / includestdio.h void main() char charArray8 F,r,i,e,n,d,s int i for(i 0 i 7 i) printf(charArrayd has a value of cn,i, charArrayi) printf(nMy favourite comedy sitcom is snn, charArray) SAMPLE PROGRAM 4 /4. Program consists of arrays (i.e. integer array and character array). It uses loops to sort the integer array elements in ascending and descending order. Then it displays the contents of the array elements respectively. / includestdio.h void main(void) int array10 0,1,2,3,4,5,6,7,8,9, i char string20 This is a string for(i…

    • 1259 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    History of Number One

    • 2633 Words
    • 11 Pages

    −1 0 1 2 3 4 5 6 7 8 9 →List of numbers — Integers0 10 20 30 40 50 60 70 80 90 →…

    • 2633 Words
    • 11 Pages
    Powerful Essays
  • Powerful Essays

    A smart phone is definitely a mini computer that will do the same as a larger computer, as well as work as a phone and a camera. The smart phone processes information through aps to help a user to work, or just to be entertained, as well as surf the internet.…

    • 1161 Words
    • 5 Pages
    Powerful Essays