Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Sparse Matrix - need help. Array index of out bounds: 1

  1. #1
    xelub is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Sparse Matrix - need help. Array index of out bounds: 1

    hi there. first time here. im getting an array index out of bounds on my code. keep checking and simulating it but there no error on my simulating. when i run it, it gives an error arrays out of bounds: 1
    any help??
    this is my code.
    Java Code:
    import java.util.Scanner;
    public class Sparse {
    	 int num = 0;
    	 public void Count_Nonzero(int Arr1[][],int m, int n)
    	{
    		int Row = 0;
    		while(Row<m)
    		{
    			int Col= 0;
    			while(Col<n)
    			{
    				if (Arr1[Row][Col] != 0) 
    				{
    					num++;
    				}
    				Col++;
    			}
    			Row++;
    		}
    		
    	}
    	 
    	
    	public static void main(String[] args) {
    		Sparse Keyboard = new Sparse();
    		Scanner scan= new Scanner(System.in);
    		System.out.println("Input rows");
    		int m=scan.nextInt();
    		System.out.println("Input Column");
    		int n=scan.nextInt();
    		 int arr1[][] = new int [m][n];
    		for(int i=0; i<m;i++){
    			for(int j=0;j<n;j++){
    				System.out.println("Enter Element ["+i+"]["+j+"]");
    				arr1[i][j]=scan.nextInt();
    				
    			}	
    		}
    		Keyboard.sparse(arr1,m,n);	
    		
    	}
    
    
    	 public void sparse(int Arr1[][],int m, int n)
    	{
    		
    		int ArrNew[][] = new int [num+1][3];
    		ArrNew[0][0]= m;
    		ArrNew[0][1]= n;
    		ArrNew[0][2]= num;
    		int Row = 0;
    		int I=1;
    		while (Row<m)
    		{
    			int Col=0;
    			while(Col<n)
    			{
    				if(Arr1[Row][Col] != 0)
    				{
    				 ArrNew[I][0] = Row;
    				 ArrNew[I][1] =Col;
    				 ArrNew[I][2] = Arr1[Row][Col];
    				I++;
    				}
    				Col++;
    			}
    			Row++;
    		}
    		for(int i=0; i<m;i++){
    			for(int j=0;j<n;j++){
    				System.out.print("|"+ArrNew[i][j]+"| ");
    			}	
    			System.out.println();
    		}	
    	}	
    
    }
    Last edited by JosAH; 01-23-2012 at 07:11 PM. Reason: added [code] ... [/code] tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Sparse Matrix - need help. Array index of out bounds: 1

    Please use code tags when posting code.
    What is the full exception you are getting, and from what line of code?
    xelub likes this.

  3. #3
    xelub is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Sparse Matrix - need help. Array index of out bounds: 1

    sorry. how can you use code tags? newbie here.
    the line : Keyboard.sparse(arr1,m,n);
    and line : ArrNew[I][0] = Row;
    are the problems. getting exception.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Sparse Matrix - need help. Array index of out bounds: 1

    I fixed the tags for you. b.t.w. those Exceptions also mention the line number where the Exception was thrown.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Mr.Java is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Thumbs up Re: Sparse Matrix - need help. Array index of out bounds: 1

    Make follwing change to remove runtime Error

    int ArrNew[][] = new int [m*n][3];
    instead
    //int ArrNew[][] = new int [num+1][3];
    Also make some change to output code block
    for(int i=0; i<m;i++){
    for(int j=0;j<n;j++){
    System.out.print("|"+ArrNew[i][j]+"| ");
    }
    System.out.println();
    }
    coz ArrNew[][] now have more colum
    and more rows.......

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Sparse Matrix - need help. Array index of out bounds: 1

    Quote Originally Posted by Mr.Java View Post
    Make follwing change to remove runtime Error

    int ArrNew[][] = new int [m*n][3];
    That way you get a 'sparse' array that takes more memory than a dense array; bad idea.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. array index out of bounds
    By searcher in forum New To Java
    Replies: 3
    Last Post: 01-21-2012, 02:27 PM
  2. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 11:27 AM
  3. Array index out of bounds error
    By blackstyle18 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 02:37 AM
  4. Sparse matrix in java
    By Zerpol in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 12:35 PM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •