Comparing, sorting Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Comparing, sorting Arrays

  1. #1
    Join Date
    Mar 2003
    Posts
    2

    Comparing, sorting Arrays

    Hello
    What I want todo is:
    Create 2 Arrays one by myself and one with unic numbers and then compare them and show how many of them are the same. It's a lottery

    I've done so one of the Arrays get unic numbers but now I wonder how do I sort them?, like 1, 20, 30?

    And also how to display how many of the arrays that matched eachother?

    Would be great with some help here

    //Ceeron from Sweden

    Code:
    public class inlamn {
    
    	public static void main(String[] args) {
    
    
    	int i, j, lottonummer;
    	int minArr[] = new int[7];
    	minArr[0] = 11;
    	minArr[1] = 25;
    	minArr[2] = 8;
    	minArr[3] = 2;
    	minArr[4] = 31;
    	minArr[5] = 23;
    	minArr[6] = 17;
    	int lottoArr[] = new int[7];
    
    	System.out.print("Your row: ");
    
    i = 0;
    while ( i < minArr.length )
    {
    	System.out.print(minArr[i] + ", ");
    	i++;
    }
    
    
    System.out.println();
    System.out.print("Lottery row: ");
    
    i = 0;
    while ( i < lottoArr.length )
    {
    
    	j = 0;
    	lottonummer = 1 + (int)(35 * Math.random());
    
    		while ( j < i && i > 0 )
    		{
    			if ( lottoArr[j] == lottonummer )
    			{
    				lottonummer = 1 + (int)(35 * Math.random());
    				if ( j > 0 )
    				{
    					j--;
    				}
    			}
    			else
    			{
    				j++;
    			}
    		}
    
    	lottoArr[i] = lottonummer;
    	System.out.print(lottoArr[i] + ", ");
    	i++;
    
    }
    
    }
    
    }

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    You basically need to look at the java.util.Arrays class. It has lots of really good generic array functions. He's a quick bit of code I knocked up (I don't recommend this as good Object-Oriented code... )
    Code:
    import java.util.Arrays;
    
    public class inlamn {
    
    	public static int[] createMyNumbersArray() {
    		int[] arr = new int[7];
    		arr[0] = 11;
    		arr[1] = 25;
    		arr[2] = 8;
    		arr[3] = 2;
    		arr[4] = 31;
    		arr[5] = 23;
    		arr[6] = 17;
    		// Now sort the array of numbers;
    		Arrays.sort(arr);
    		return arr;
    	} 
    
    	public static int[] createLottoArray() {
    		int maxLottoNumber = 35;
    		int[] arr = new int[7];
    		int randomNumber;
    
    		for (int i=0; i < arr.length; i++) {
    			randomNumber = 1 + (int)(maxLottoNumber * Math.random());
    			while (contains(arr, randomNumber)) {
    				randomNumber = 1 + (int)(maxLottoNumber * Math.random());
    			}
    			arr[i] = randomNumber;	
    		}
    
    		// Now sort the array of random numbers;
    		Arrays.sort(arr);
    		return arr;
    	}
    
    	public static boolean contains(int[] arr, int number) {
    		for (int i=0; i < arr.length; i++) {
    			if (arr[i] == number) {
    				return true;
    			}
    		}
    		return false;
    	}
    
    	public static String getArrayString(int[] arr) {
    		String returnString = "";
    
    		int arraySize = arr.length;
    		for (int i = 0; i < arraySize; i++) {
    			returnString += arr[i];
    			if (i < arraySize - 1) {
    				returnString += ",\t";
    			} 
    
    		}
    
    		return returnString;
    	}
    
    	public static int countMatches(int[] arr1, int[] arr2) {
    		int count = 0;
    		for (int index=0; index < arr1.length; index++) {
    			if (contains(arr2, arr1[index])) { 
    				count++;
    			} 
    		}
    
    		return count;
    	}
    
    	public static void main(String[] args) {
    		int minArr[] = createMyNumbersArray();
    		int lottoArr[] = createLottoArray();
    
    		System.out.println("Your row:       " + getArrayString(minArr));
    		System.out.println("Lottery row:    " + getArrayString(lottoArr));
    		System.out.println("Number Matched: " + countMatches(minArr, lottoArr));
    	}
    
    }
    Hope this helps,

    ArchAngel.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Mar 2003
    Posts
    2
    Do you really have to use all those different methods and such? As u can see in the code I wrote I'm a n00b and my knowledge is justin the basic about loops and ifs and such. I don't know so much about all those things u are making
    From the code I wrote could u tell me a little way to accoplish something similair with n00bish code?

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    OK. The two bits of my code you need to understand are:

    1. Arrays.sort(arr);
    This is a static (i.e. a class) method which takes an array of ints and sorts them. It's that easy.


    2. My 'contains()' method:

    Code:
    	public static boolean contains(int[] arr, int number) {
    		for (int i=0; i < arr.length; i++) {
    			if (arr[i] == number) {
    				return true;
    			}
    		}
    		return false;
    	}
    This method checks an array of ints ('arr') for a certain number ('number'). If that number is present in the array, 'true' is returned, otherwise 'false' is retrurned.

    There are other Java libraries that you could use to do this search, but they have some subtle features.

    As an aside, if you look through my code you should see that the only extra Java feature that I use is the method definition. I recommend you try and understand it as it allows you to split up your code and make it easier to read.

    Hope this helps,

    ArchAngel.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center