Solved

Doble array execution

Posted on 2004-04-27
15
354 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
ID: 10932652
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
ID: 10932699
You also have to spell 'length' correctly. :)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10932700
Also, System.out.println is not defined for int[][].
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10932739
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
ID: 10932772
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
ID: 10932954
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
ID: 10933031
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10933039
(import java.util.Arrays)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10933325
>> 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
ID: 10933337
What a nice point split!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10933359
>>What a nice point split!

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

Expert Comment

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

Expert Comment

by:mark-b
ID: 10942232
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
ID: 11045511
I guess that's not happening! :-(
0
 
LVL 7

Expert Comment

by:mark-b
ID: 11045553
Guess not.  Sorry guys!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
tomcat startup error 5 65
spring jars download 1 28
Is there a simpler dropbox system? 10 22
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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