This Program Illustrates What Happens If an Attempt Is Made

This Program Illustrates What Happens If an Attempt Is Made

<p>// ******************************************************************* // ArrayOpeartions7.java By: Aiman Hanna (C) 1993 - 2016 // // This program illustrates what happens if an attempt is made // to access the array out of its bounds. Notice that this // program does not work. WHY? // // Key Points: 1) Array - Out-of-Bound Problem // ******************************************************************* import java.util.Scanner; public class ArrayOperations7 {</p><p> public static void main (String[] args) {</p><p> int i, v; int count = 0; Scanner kb = new Scanner(System.in);</p><p>// create two arrays int[] ar1 = new int[16], ar2 = new int[10];</p><p>// Just initialize the first arrays with some values for (i = 0; i < ar1.length; i++) { ar1[i] = i * 5;</p><p>}</p><p>// Now copy the ar1 to ar2 for (i = 0; i < ar1.length; i++) // Notice that ar1 is bigger than ar2 { ar2[i] = ar1[i];</p><p>}</p><p>// Show the contents of the arrays System.out.println("Here are the values in the array ar1: "); for (i = 0; i < ar1.length; i++) { System.out.print(ar1[i] + " "); }</p><p>System.out.println("\nHere are the values in the array ar2: "); for (i = 0; i < ar2.length; i++) { System.out.print(ar2[i] + " "); }</p><p>// Now change some of the values of ar2 ar2[3] = 980; ar2[5] = 609; ar2[7] = 1200;</p><p>System.out.print("\nPlease enter the value that you wish to search for in ar1: "); v = kb.nextInt(); for (i = 0; i < ar1.length; i++) { if (ar1[i] == v) { System.out.println("Value was found at index: " + i); count++; } } // If you are here and count is still 0, then the value was not found if (count == 0) { System.out.println("Sorry; value was not found in array ar1."); }</p><p>// Show the contents of the arrays again! System.out.println("\nHere are the values in the array ar1: "); for (i = 0; i < ar1.length; i++) { System.out.print(ar1[i] + " "); }</p><p>System.out.println("\nHere are the values in the array ar2: "); for (i = 0; i < ar2.length; i++) { System.out.print(ar2[i] + " "); } kb.close();</p><p>} }</p><p>/* The result of program compilation </p><p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at ArrayOperations7.main(ArrayOperations7.java:37) */</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us