Difference between revisions of "Equating Two Arrays"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Equating Two Arrays
 
{{1010PrAD|ProblemName=Equating Two Arrays
  
|Problem= This problem envolves you seeing if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use: <pre> array.length</pre> Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this.
+
|Problem= This problem envolves you seeing if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use: <pre> array.length</pre> Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this. If you can try to structure your code so that your loop is only comparing to the length of one of the arrays and not both of them.  
  
 
|SideSectionTitle=...By Students
 
|SideSectionTitle=...By Students
Line 13: Line 13:
 
   public static void main (String[]args)
 
   public static void main (String[]args)
 
   {
 
   {
     int[] array1 = {1,2,3,4,5,6,7,8,9,10};
+
     int[] array1 = {1,2,3,4,5,6,7,8,9,10}; //First array to be equated
     int[] array2 = {1,2,3,4,5,6,7,8,9,0};
+
     int[] array2 = {1,2,3,4,5,6,7,8,9,0};   //Second array to be equated
     boolean equal = true;
+
     boolean equal = true;                   //boolean to say whether the arrays are equal or not.
 
      
 
      
 +
    //Check to see if the arrays are equal in lenght, if they are not the arrays can not be equal.
 
     if (array1.length == array2.length)
 
     if (array1.length == array2.length)
 
     {
 
     {
 +
      //Use a for loop to iterate through the arrays, comparing their values.
 
       for (int i = 0; i < array1.length; i++)
 
       for (int i = 0; i < array1.length; i++)
 
       {
 
       {
 +
        //If there is a mismatch, they are not equal.
 
         if (array1[i] != array2[i])
 
         if (array1[i] != array2[i])
 
           equal = false;
 
           equal = false;
Line 27: Line 30:
 
     else
 
     else
 
       equal = false;
 
       equal = false;
     
+
   
 +
    //Print out the status of the arrays to the console.
 
     if (equal)
 
     if (equal)
 
       System.out.println ("The two arrays are equal.");
 
       System.out.println ("The two arrays are equal.");

Revision as of 09:40, 8 April 2010

Back to the Program-A-Day homepage

Problem

This problem envolves you seeing if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use:
 array.length
Remember that your arrays start indexing at 0 and that you should watch out for loops indexing arrays out of bounds. These are the most common pitfalls with a problem like this. If you can try to structure your code so that your loop is only comparing to the length of one of the arrays and not both of them.
 

...By Students

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage