Solved

Doble array execution

Posted on 2004-04-27
15
353 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 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

22 Experts available now in Live!

Get 1:1 Help Now