2

I have a 2-d array of double numbers it's 48 by 48, I am trying to make a method that will allow the user to select a specific amount e.g. = 7 by 7 and then put that into a new 2d array.

public static double[][] amountOTP(double [][] a, int x){
    a = new double[x][x];
    return a;
}

thats all i have right now, this takes an 2d array as input however even though i specified x it doesn't work.

9
  • What exactly should the new array contain? Commented Apr 16, 2017 at 17:40
  • 2
    "it doesn't work" - what does that mean? What do you expect to happen, what happens instead? Commented Apr 16, 2017 at 17:41
  • Do you mean that you wish to cut off the size of the 2d array to that of the number specified by the user? (So basically make the 2d array smaller, but have it keep its values?) Commented Apr 16, 2017 at 17:41
  • @AlexanderDaum my 2d array matrix is 48 by 48, basically i want this method to make a new 2d array that contains only 7 by 7 of that data from the original 2d array Commented Apr 16, 2017 at 17:42
  • 1
    @YorickIsTheBest yes sir Commented Apr 16, 2017 at 17:42

6 Answers 6

2

When you want to cut it ro a smaller size and copy the part of the original array, this should work:

public static double [][] cutArray (double [][] a, int newSize){
  if (x > a.length)
     throw new IllegalArgumentException ("Can only make array smaller");
  double [][] b = new double [newSize][newSize];
  for (int i = 0; i < newSize; i++){
    for (int j = 0; j < newSize; j++){
      b [i][j] = a [i][j];
    }
  }
  return b;
}
Sign up to request clarification or add additional context in comments.

Comments

2

The solution below considers situations in which the requested new two-dimensional array length is greater than the original in which case we simply return the original.

Example:

public static double[][] amountOTP(double [][] a, int x){
       if(x > a.length) return a;
       for (double[] arr : a)
          if(arr.length < x)
             return a;

       double[][] newArray = new double[x][x];
       for (int i = 0; i < x; i++)
          for (int j = 0; j < x; j++)
             newArray[i][j] = a[i][j];

       return newArray;
}

1 Comment

description does not match code: "requested length ... is less than original" should read "is greater"
2

Sorry if I'm wrong, but I'm assuming for now that you want your method to return a smaller 2d array which contains some of the values of the given array, in which case you would need to change your method to this:

public static double[][] amountOTP(double [][] a, int x) {
   double[][] b = new double[x][x];
   x = Math.min(x, a.length);
   for(int i = 0;i < x; i++)
      for(int j = 0;j < x; j++)
         b[i][j] = a[i][j];
   return b;
}

This should work fine but feel free to comment and inform me if I left anything out or it doesn't work; I'm here to help. Anyways, I hope this works for you. (Also, if this isn't the answer you were looking for, feel free to tell me.)

Note: This should avoid IndexOutOfBounds exceptions, so it will still work correctly if the user gives an x value bigger than the size of a. The 2d array that this method returns will just have some values of zero where it couldn't find any numbers.

6 Comments

@JokesOnyou No problem! Glad I could help.
@OsumaneMahyDiaw If the length is less than the original array, then yes. The values will be copied. (Not all of them however; just the ones that can fit.) And I had assumed that the 1d arrays inside a were all the same size and equal to the length of a itself, making the 2d array a square. If the OP requires it, I will edit my answer to accommodate for a matrix which is not a square.
x<a.length?x:a.length use Math.min or at least add some spaces.
@PatrickParker forgot about spaces so I'll add that now, but Math.min? x is an integer...
@YorickIsTheBest yes it works with ints too. some might prefer it over ternary operator for clarity, especially when answering beginners
|
1

I think it's something like that you're looking for :

public static double[][] amountOTP(double [][] a, int x){
    double [][] ret = new double[x][x];

    for(int i = 0; i < x; i++)
        for(int j = 0; j < x; j++)
            ret[i][j] = a[i][j];
    return ret;
}

But you have to be careful with the parameters because it can cause an IndexOutOfBounds exception

Comments

0

I see you already have a lot of answers, but nobody was making use of the excellent Arrays.copyOf Java API method:

public static double[][] amountOTP(double [][] a, int x){
    a = Arrays.copyOf(a, x);
    for(int i=0; i<a.length; i++) {
        if(a[i] != null) {
            a[i] = Arrays.copyOf(a[i], x);
        } else {
            a[i] = new double[x]; // allows growth
        }
    }
    return a;
}

Comments

0

you are actually not yet initializing the array so the method should be like this

public static void main(String[] args)
    {
        double[][] a=amountOTP(3);
        for(int j=0;j<a.length;++j)
        {
            for(int i=0;i<a[j].length;++i)
            {
                a[j][i]=2;
            }
        }
        for(int j=0;j<a.length;++j)
        {
            for(int i=0;i<a[j].length;++i)
            {
                System.out.println(a[j][i]);
            }
        }


    }
    public static double[][] amountOTP(int x)
    {
        return new double[x][x];

    }

1 Comment

@JokesOnyou if this help, you can accept it as an answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.