The Artima Developer Community
Sponsored Link

Java Answers Forum
help!

5 replies on 1 page. Most recent reply: Mar 30, 2003 5:26 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

help! Posted: Mar 30, 2003 9:13 AM
Reply to this message Reply
Advertisement
i am stuck at this particular program. How can i split an original array into 2? One is with even numbers and the other odd. For eg, how do i split{5,9,12,32,25,17} into 2 separate arrays in java? Pls help by giving me some clues..thanx!


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: help! Posted: Mar 30, 2003 12:23 PM
Reply to this message Reply
I have mentioned two methods. One is in main method and other is in anotherMethod. Use whichever you want to. This will give you at least an idea about how to approach this problem.
import java.util.*;
 
 
 
public class Test1 {
 
	public static void main (String[] args) throws Exception {
 
		int[] source = {5,9,12,32,25,17} ;
 
		int[] even;
		int[] odd;
 
		int evenCounter = 0;
		int oddCounter  = 0;
 
		// Get the number of odd and even values so that
		// we can create the array of that length
		for ( int i = 0 ; i < source.length; i++ ){
			if ( source[i] % 2 == 0 ) {
				evenCounter++;
			}
			else {
				oddCounter++;
			}
		}
 
		even = new int[evenCounter];
		odd = new int[oddCounter];
 
		// Prase the whole array
		evenCounter = 0;
		oddCounter = 0;
		for ( int i = 0 ; i < source.length; i++ ){
			if ( source[i] % 2 == 0 ) {
				even[evenCounter++] = source[i];
			}
			else {
				odd[oddCounter++] = source[i];
			}
		}
 
		// Print even numbers
		System.out.print( "Even numbers:" ) ;
		printArray (even);
 
		// Print odd numbers
		System.out.print("Odd numbers:" ) ;
		printArray (odd);
 
		System.out.println ( "Another method...." );
		anotherMethod();
	}
 
 
	public static void printArray(int[] arr) {
 
		for ( int i = 0 ; i < arr.length; i++ ) {
			System.out.print ( arr[i] + "  " ) ;
		}
 
		System.out.println () ;	// A blank line
	}
 
	public static void anotherMethod ( ) {
		int[] source = {5,9,12,32,25,17} ;
 
		ArrayList even = new ArrayList();
		ArrayList odd  = new ArrayList();
 
		// Get the number of odd and even values so that
		// we can create the array of that length
		for ( int i = 0 ; i < source.length; i++ ){
			if ( source[i] % 2 == 0 ) {
				even.add( new Integer(source[i]));
			}
			else {
				odd.add( new Integer(source[i]));
			}
		}
 
		// If you need array of ints for even and odd
		// you need to loop thru even and odd arrayList and store
		// int values in array. I am just printing the values
		// in array list below
 
		System.out.println ( "Even numbers:" + even );
		System.out.println ( "Odd numbers:" + odd );
	}
 
}

Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Re: help! Posted: Mar 30, 2003 12:35 PM
Reply to this message Reply
Well, actually this is what i have done but it does not seem to work, can anyone explain? Thanx

 
class OddEven
{
public static void main(String[] args) {
int[] input = {10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8};
 
int[][] output = split(input);
 
System.out.print("original: 10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8");
 
for (int i = 0; i < input.length; ++i) { 
if ( i % 2 == 0);
	System.out.print(" even: " + i);
 
if ( i % 2 == 1);
	System.out.print(" odd: " + i);
} 
 
public static int[][] split(int[] ori) {
 
int i,even = 0,odd = 1;
int result[][]; 
 
for(i = 0; i < ori.length; ++i){
	if( ori % 2 == 0) { 
		result[0][even] = ori;
		even++;
	}
	else {
		result[1][odd] = ori;
		odd++;
	}
return result;
	}
}
 

Rich Burgis

Posts: 17
Nickname: songbird
Registered: Mar, 2003

Re: help! Posted: Mar 30, 2003 1:16 PM
Reply to this message Reply
As I see it you are trying (I'm guessing here since you aren't displaying your output array) to create an array that looks like

10 6 2 9 33 9
5 1 17 4 11 8

(or maybe
5 1 9 17 33 11 9
10 6 2 4 8 )

If that's the case you need to do a couple of things. First and foremost you need to actually create the output array. You don't do that

When you say

int result[ ] [ ];

you declare the array, but you haven't reserved any space for it.

you need to say something like

int result[ ][ ] = new int[ 2 ][ ori.length / 2 ];

(in the alternative case you'd say new int[ 2] [ ori.length ]; )

I suspect you're getting a runtime out of bounds error. This will fix it.

Second you set the two indexes as
int even = 0, odd = 1;

If you want them to be independent idexes to the two parts of the array they should both start at zero. (Better you should have one index and use it for both, but that will cause unnecessary complication at this point.)

Finally what is ori % 2?

if you want to test if the element is even or odd you need to use an index. i.e. ori[ i ] % 2.

Good luck
Rich
> Well, actually this is what i have done but it does not
> seem to work, can anyone explain? Thanx
>
>
> class OddEven
> {
> public static void main(String[] args) {
> int[] input = {10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8};
> 
> int[][] output = split(input);
> 
> System.out.print("original: 10, 5, 6, 1, 2, 17, 9, 4, 33,
> 11, 9, 8");
> 
> for (int i = 0; i < input.length; ++i) {
> if ( i % 2 == 0);
> System.out.print(" even: " + i);
> 
> if ( i % 2 == 1);
> System.out.print(" odd: " + i);
> }
> 
> public static int[][] split(int[] ori) {
> 
> int i,even = 0,odd = 1;
> int result[][];
> 
> for(i = 0; i < ori.length; ++i){
> if( ori % 2 == 0) {
> result[0][even] = ori;
> even++;
> }
> else {
> result[1][odd] = ori;
> odd++;
> }
> return result;
> }
> }
> 
> 

Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Re: help! Posted: Mar 30, 2003 1:33 PM
Reply to this message Reply
welll actually...my errors are as below

OddEven.java:18: illegal start of expression
public static int[][] split(int[] ori) {
^
OddEven.java:34: ';' expected
}
^
OddEven.java:6: cannot resolve symbol
symbol : method split (int[])
location: class OddEven
int[][] output = split(input);
^
3 errors

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: help! Posted: Mar 30, 2003 5:26 PM
Reply to this message Reply
Here is what you are attempting to get.
class OddEven {
	public static void main(String[] args) {
			int[] input = {10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8};
			int[][] output = split(input);
 
			System.out.print("original: 10, 5, 6, 1, 2, 17, 9, 4, 33, 11, 9, 8");
			for (int i = 0; i < input.length; ++i) {
				if ( i % 2 == 0)	// Took out semi-colon
					System.out.println(" even: " + i);
 
				if ( i % 2 == 1)	// took out semi-colon
					System.out.println(" odd: " + i);
			}
}
 
	public static int[][] split(int[] source) {
 
			int result[][] = new int[2][];
 
			// Get odd and even numbers
			int[] even;
			int[] odd;
			int evenCounter = 0;
			int oddCounter  = 0;
			// Get the number of odd and even values so that
			// we can create the array of that length
			for ( int i = 0 ; i < source.length; i++ ){
				if ( source[i] % 2 == 0 ) {
					evenCounter++;
				}
				else {
					oddCounter++;
				}
			}
 
			even = new int[evenCounter];
			odd = new int[oddCounter];
			// Parse the whole array
			evenCounter = 0;
			oddCounter = 0;
			for ( int i = 0 ; i < source.length; i++ ){
				if ( source[i] % 2 == 0 ) {
					even[evenCounter++] = source[i];
				}
				else {
					odd[oddCounter++] = source[i];
				}
			}
 
			result[0] = odd;
			result[1] = even;
			
			return result;
 
	}
 
	
}

Flat View: This topic has 5 replies on 1 page
Topic: Performance improvement Previous Topic   Next Topic Topic: The use of MouseListener in a JTable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use