Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with this please :)

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with this please :)

    import java.util.Scanner;
    import java.io.*;
     
    public class Part5{
     
        public static int [][] getSubMat(int [][] data, int row, int col, int width, int height){
     
    int [][] newMat = new int [height][width];
     
    for(int g = 0; g < newMat.length ; g++){
        for(int h = 0; h < newMat.length; h++){
            for(int i = row; i < height +row; i++){
                for(int j = col; j < width+col; j++){
            newMat[g][h]=data[i][j];
                        }
                    }
                }
            }
     
            return null;
        }
        public static void main (String [] args){
            Scanner input = new Scanner(System.in);
     
            int [][] list = {{1,2,3,4,5},
                             {6,7,8,9,10},
                             {11,12,13,14,15},
                             {16,17,18,19,20}};
     
            getSubMat(list,1,2,2,2);
     
        }
    }

    im trying to extract this matrix and putting into a submatrix but i keep getting 14 on every location of the new matrix... help... but when i go..

      for(int i = row; i < height +row; i++){
                for(int j = col; j < width+col; j++){
                         System.out.printf("%2d" + " " ,data[i][j]);
            }
    }
    on my for loop. i get exactly the numbers that i want.. help

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Help with this please :)

    Why on earth do you have a 4-nested for loop? What do you think that's doing?

    PS- In the future, you probably want to use more meaningful post titles. Generic titles like "help with this please" tend to be ignored. We know you need help. What kind? A title like "extracting specific section of 2D array" would have been a lot more helpful.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!