0

I have a question about stop array input limit then the result to show the output.

Below is my coding have already set new float[3][3]:

    import java.util.Scanner;


      public class Clone2Darray {
    public static float[][] clone(float[][] a) throws Exception {
      float b[][] = new float[a.length][a[0].length];

        for (int i = 0; i < a.length; i++) {
          for (int j = 0; j < a[0].length; j++) {
               b[i][j] = a[i][j];
          }
       }
       return b;
     }
     public static void main(String args[]) {

               Scanner sc = new Scanner (System.in);
                     System.out.println ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
         float[][] a = new float[3][3];
          for (int i=0; i<3; i++){
             for (int j=0; j<3; j++){
                 String line = sc.nextLine();
                if ("-1".equals(line)){
                      break;
                  }
                  a[i][j]=Float.parseFloat(line);
              }
           }

              System.out.println("\n The result is:");

              try {
            float b[][] = clone(a);
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    System.out.print(b[i][j] + " ");
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println("Error!!!");
        }
         }
      }

The limit output is show me like below:

 run:
 Type float numbers in the two-dimensional array of similar type and size 
  with line breaks, end by -1:

  5.33
  9.33
  63.33
  6.36
  3.55
  7.25
  2.33
  3.66

  The result is:
  6.33 5.33 9.33 
  63.33 6.36 3.55 
  7.25 2.33 3.66 
  BUILD SUCCESSFUL (total time: 31 seconds)

My problem is want to stop limit float[3][3] and can unlimited key in the input until type -1 to stop the input. May I know how to remove the limit float[3][3] in the array? Hope anyone can guide me to solve my problem. Thanks.

2 Answers 2

1

At the point when you allocate memory for the two-dimensional array you have to tell the sizes of its elements, because memory will be allocated for that array and the amount of memory to be allocated must be known.

You can bypass this, by using some more dynamic types, like List and its popuplar implementation, ArrayList, even in a nested form. That's a nice thing to do, but then you will not have a "real" array.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you edit from my code to show me how works is it?
@CHSon for that purpose I would need to know how you intend it to do. I do not understand your exact goal, but I understand that you do not want to tie yourself to a decision of sizes. You can see an example here about ArrayList of ArrayList: geeksforgeeks.org/arraylist-of-arraylist-in-java It's not very far from your goal, I suppose.
1

The below code allows you to create the dynamic arrays.


import java.util.Scanner;


public class Clone2DArray {
public static float[][] clone(float[][] a) throws Exception {
float b[][] = new float[a.length][a[0].length];

  for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[0].length; j++) {
         b[i][j] = a[i][j];
    }
 }
 return b;
}
public static void main(String args[]) {

         Scanner sc = new Scanner (System.in);
         System.out.println("enter row size");
         int row =  Integer.parseInt(sc.nextLine());
         System.out.println("enter column size");
         int column = Integer.parseInt( sc.nextLine());

               System.out.println ("Type float numbers two-dimensional array of similar type and size with line breaks:");
   float[][] a = new float[row][column];
    for (int i=0; i<row; i++){
       for (int j=0; j<column; j++){
           String line = sc.nextLine();
          if ("-1".equals(line)){
                break;
            }
            a[i][j]=Float.parseFloat(line);
        }
     }

        System.out.println("\n The result is:");

        try {
      float b[][] = clone(a);
      for (int i = 0; i < a.length; i++) {
          for (int j = 0; j < a[0].length; j++) {
              System.out.print(b[i][j] + " ");
          }
          System.out.println();
      }
  } catch (Exception e) {
      System.out.println("Error!!!");
  }
   }
}

1 Comment

but why press 2 times -1 then stop input?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.