Solved

Doble array execution

Posted on 2004-04-27
15
352 Views
Last Modified: 2009-12-16
I am trying to find the array that results from the execution of (3, 4, 21)
Where the code for the method that was given to me is:

public static int [] []  f(int n, int m, int value);
{
        int [] [] x = new int [n] [m];
        for (int i=0; i < x.lenght; i++)
        for (int j = 0; j < x[i].lenght; j++)
        x[i] [j] = value;
        return x;
}
      

Down below you can find how tried to run it.... I am missing something but do not know what.  Can someone help me with this?  Thank you!   J.Cisek - Java student.


public class SkillBuilder
{

    int [] [] f(int n, int m, int value);
   

    public static void main(String[] args)
    {
       
        int [][] x = new int [n] [m];
        for (int i=0; i < x.lenght; i++)
        for (int j = 0; j < x[i].lenght; j++)
        x[i] [j] = value;
        return x;
        }

    System.out.println( x (x(int n, int m, int value) ) );
0
Comment
Question by:j_cisek
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
You can't return something from a method that declares that it returns void.

You can't ever return anything from main(). Put the code you have written in a method with a different name than main() and make sure it returns in[][]. Furthermore, there are no prototype declarations in Java (Java is not C ;-).
0
 
LVL 7

Expert Comment

by:mark-b
Comment Utility
You also have to spell 'length' correctly. :)
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
Also, System.out.println is not defined for int[][].
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
This should get you on your way:

      public static int[][] populateArrays(int n, int m, int value) {
            int[][] x = new int[n][m];
            for (int i = 0; i < x.length; i++) {
                  for (int j = 0; j < x[i].length; j++) {
                        x[i][j] = value;
                  }
            }
            return x;
      }
0
 
LVL 7

Accepted Solution

by:
mark-b earned 500 total points
Comment Utility
probably what you want is something like this:

public class SkillBuilder {

    private static int [] [] f(int n, int m, int value) {
        int [][] x = new int [n] [m];
        for (int i=0; i < x.length; i++) {
         for (int j = 0; j < x[i].length; j++) {
            x[i] [j] = value;
         }
        }

        return x;
    }
   
    public static void main(String[] args)  {
      int[][] x = f[Integer.parseInt( args[0] ), Integer.parseInt(args[1] ), Integer.parseInt(args[2] ) );
   
      // do something with x
    }
0
 

Author Comment

by:j_cisek
Comment Utility
Dear orangehead911,

Thank you for your comments there are very useful, especially that you remind me that I have to be more detail oriented.  My next question is how to print out the resultant array.
Do I have to write a String toString() method to print it?  If yes, can this method be inside of a public static void main(String[] args)?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can slightly simplify the first part. Here also is a toString:


      private static int[][] f(int n, int m, int value) {
            int[][] x = new int[n][m];
            for (int i = 0; i < x.length; i++) {
                  Arrays.fill(x[i], value);
            }
            return x;
      }


      public String toString(int[][] array) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; i++) {
                  for(int j = 0; j < array[i].length - 1; j++) {
                        sb.append(array[i][j]);
                        sb.append(",");
                  }
                  sb.append(array[i][array[i].length - 1]);
                  sb.append("\n");
            }
            return sb.toString();
      }

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(import java.util.Arrays)
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
>> Do I have to write a String toString() method to print it?
The toString() method is a method you would override in a class that you've implemented to provide useful information about the state of the class instance. In your case you don't have a class, so the answer is no. You would want to implement functionality similar to toString(). Why not use the code posted by CEHJ above?!?

>> If yes, can this method be inside of a public static void main(String[] args)?
You can never place a method definition within another method definition(*). You can define a method for the printing of the values outside the main method (must also be static) and call that method from inside your main method.
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
What a nice point split!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>What a nice point split!

Yes. Looks like some have come out of that rather badly ;-)
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
Indeed, I guess that's what you get for providing good help eh? =-)
0
 
LVL 7

Expert Comment

by:mark-b
Comment Utility
j_cisek,

Could you please reopen this question it and distribute the points 3-ways between CEHJ, orangehead911, and myself?

Thanks,

-Mark
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
I guess that's not happening! :-(
0
 
LVL 7

Expert Comment

by:mark-b
Comment Utility
Guess not.  Sorry guys!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now