Skip to main content

Arrays in Java (Part 1)

>

Arrays in Java: (Part 1)

Introduction:

  • So far, we have only used simple variables to store values of different data types. However, in certain situations, it will be easier to write programs that uses list type structure to store similar type of data.
  • For example, if we want to record the student marks for a class of 60 students, we have to create variables like student_1, student_2, student_3… student_60. (Similarly, if we are to store the details like Student_Name, Student_Email of N number of students, its hard to store information in variables) This is tedious and it increases the complexity and length (number of Lines Of Code: LOC of programs).
  • This problem can be simplified by using arrays that uses a common name with a subscript representing the index of each element. If we use array for storing marks of 60 students, it will go like this: student[1], student[2], student[3]… student[60].
  • An array is an ordered sequence of finite data items of the same data type that shares a common name. It is a collection of similar kind of data type (or it hold values of homogeneous type). Each individual data item of array is known as element of array. These elements are stored in the subsequent memory location starting from the first memory block.
  • Index of array is the position of array element. The index (or location) of array starts from zero always (0). So we can say that the first element of array is at position 0 in array. That is if I want to refer to the first student, it will be at location 0 (e.g, student[0]=first element of array, student[1] = second element of array, etc..). And the last index of array is always size-1. If the size of array is 60, the last element will be at position 59. That is student[59] = last element of array.
  • An array can be one dimensional or two dimensional or even multi-dimensional. One dimensional array uses a single index and two dimensional array uses two indexes and so on. One dimensional array has rank “one”, two dimensional array has rank “two” and so on.

Creating arrays (any dimension):

  • An array must be created before using its elements in a program (Just like our simple variables). Creating an array involves following steps:
    1. Declaration
    2. Initialization
    3. Assigning values
Click HERE for One Dimensional Arrays in Java

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...