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 4 of 4

Thread: inverting rows and lines of a matrix

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default inverting rows and lines of a matrix

    Hi everyone, I'd like to create a program in which all rows and columns of a given matrix are inverted. The matrix elements are objects Bin<Double,Integer>, where Double is the value, Integer is the priority (that can be 0 or 1). When i reorder the matrix, I have to replace the entire Objects
    For example :

    Schermata 2019-10-31 alle 06.35.18.png

    I've thought something like this, but i don't think it's quite correct:

    for(int i=0;i<matrix.length;i++){
    for(int k=0;k<matrix.length;k--) {
    for(int j=0;j<matrix.length;j++){
    for (int s = 0; s < matrix.length; s--) {

    Bin<Double,Integer> element = new Bin<>();

    element= mat_simmetrica[i][j];
    mat_simmetrica[i][j]=mat_simmetrica[k][s];
    mat_simmetrica[k][s]=element;

    }
    j++;
    }

    }
    i++;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: inverting rows and lines of a matrix

    i don't think it's quite correct:
    Please explain. Post the program's current output and add comments saying what is wrong with it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: inverting rows and lines of a matrix

    Actually it doesn't work, It gives me: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    The idea is to switch each element with his opposite, so I've thought to do 4 cycles E(i,j) <-> E(k,s)

    K=2

    E(0,0) <-> E(k,k)
    E(0,1)<->E(k,k-1)
    E(0,2)<->E(k,k-2)

    E(1,0)->E(k-1,k)
    E(1,1)->E(k-1,k-1)
    E(1,2)->E(k-1,k-2)

    E(2,0)->E(k-2,k)
    E(2,1)->E(k-2,k-1)
    E(2,2)->E(k-2,k-2)

            for(int i=0;i<mat_simmetrica.length;i++){
                    for(int k=mat_simmetrica.length;k>0;k--) {
                        for(int j=0;j<mat_simmetrica.length;j++){
                        for (int s = mat_simmetrica.length; s > 0; s--) {
     
                            Bin<Double,Integer> element = new Bin<>();
     
                            element= mat_simmetrica[i][j];
                            mat_simmetrica[i][j]=mat_simmetrica[k][s];
                            mat_simmetrica[k][s]=element;
     
                        }
                        j++;
                    }
     
                }
               i++;
            }
    Last edited by mikoile; October 31st, 2019 at 09:45 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: inverting rows and lines of a matrix

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    What variable has the -1?

    Where does the code use a negative index? Zero minus 1 = -1
    The code should not subtract from a 0 index
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. columns and rows [incomplete]
    By Rauda in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 13th, 2017, 11:43 AM
  2. rows and columns in the form of...
    By ggx7 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2014, 02:17 AM
  3. Replies: 4
    Last Post: February 22nd, 2013, 03:51 PM
  4. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  5. How to add rows dynamically to in a JSPpage
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: January 21st, 2011, 09:04 AM