重新调整数组大小(如扩展和图像)- Java

发布于 2024-12-10 15:10:46 字数 1099 浏览 0 评论 0原文

抱歉,让我重新表述一下问题,
我需要弄清楚如何将数组的每个元素移动到新数组中:就像每个元素的“x”位置将移动到“x*2”和“x*2+1”:所以 array[0 ]-> array2[0]、array2[1] 和 array[1] -> array2[2]、array2[3] 等,但对于“y”值也

对于我的Java应用程序,我需要一个函数来输入

   [[1,0,1],
    [1,0,1],
    [1,1,1]]

并复制数组并输出,

   [[1,1,0,0,1,1],
    [1,1,0,0,1,1],
    [1,1,0,0,1,1], 
    [1,1,0,0,1,1],
    [1,1,1,1,1,1],
    [1,1,1,1,1,1]]

这是我能弄清楚的

public short[][] expandArray(short[][] arr) {
   short[][] newArray = new short[arr.length*2][arr[0].length*2];
   for(int s=0; s<arr.length; s++)
      for(int ss=0; ss<arr[0].length; ss++) {
         newArray[s*2][(new corresponding row)] = arr[s][ss];
         newArray[s*2+1][(new corresponding row)] = arr[s][ss];

         newArray[s*2][(next row down)] = arr[s][ss];
         newArray[s*2+1][(next row down)] = arr[s][ss];
      }
   return newArray;
}

我的目标是复制每个元素在数组的右侧和下方

例如:
OriginalArray[0][0]

将被放入

NewArray[0][0]、NewArray[0][1]、NewArray[1][0]、NewArray[1][1]



谢谢

Sorry let me rephrase the question,
I need to figure out how to move each element of the array into the new array: like for the "x" position of each element would be moved to "x*2" and "x*2+1": so array[0] -> array2[0], array2[1] and array[1] -> array2[2], array2[3] etc. but for the "y" value also

For my Java application I need a function that inputs

   [[1,0,1],
    [1,0,1],
    [1,1,1]]

And would replicate the array and output

   [[1,1,0,0,1,1],
    [1,1,0,0,1,1],
    [1,1,0,0,1,1], 
    [1,1,0,0,1,1],
    [1,1,1,1,1,1],
    [1,1,1,1,1,1]]

here is what I can figure out

public short[][] expandArray(short[][] arr) {
   short[][] newArray = new short[arr.length*2][arr[0].length*2];
   for(int s=0; s<arr.length; s++)
      for(int ss=0; ss<arr[0].length; ss++) {
         newArray[s*2][(new corresponding row)] = arr[s][ss];
         newArray[s*2+1][(new corresponding row)] = arr[s][ss];

         newArray[s*2][(next row down)] = arr[s][ss];
         newArray[s*2+1][(next row down)] = arr[s][ss];
      }
   return newArray;
}

My goal is to duplicate each element in the array to the right and down

EX:
OriginalArray[0][0]

would be put into

NewArray[0][0], NewArray[0][1], NewArray[1][0], NewArray[1][1]

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2024-12-17 15:10:46
import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        int[][] array = { {1,0,1}, {1,0,1}, {1,1,1} };
        int[][] newArray = replicate(array);

        int i = 1;

        System.out.print("[ ");
        for(int[] a : newArray)
        {
            System.out.print(Arrays.toString(a) + (i++ != newArray.length? ", " : "") );
        }
        System.out.println(" ]");
    }

    public static int[][] replicate(int[][] array)
    {
        int x = array.length;
        int y = array[0].length;

        int counterX = 0;
        int counterY = 0;

        int[][] newArray = new int[2 * x][2 * y];
        for(int[] a : array)
        {
            for(int b : a)
            {
                newArray[counterX++][counterY] = b;
                newArray[counterX--][counterY++] = b;

                newArray[counterX++][counterY] = b;
                newArray[counterX--][counterY++] = b;
            }
            counterY = 0;
            counterX++;
            counterX++;
        }

        return newArray;
    }
}

输出:

[ [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1] ]
import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        int[][] array = { {1,0,1}, {1,0,1}, {1,1,1} };
        int[][] newArray = replicate(array);

        int i = 1;

        System.out.print("[ ");
        for(int[] a : newArray)
        {
            System.out.print(Arrays.toString(a) + (i++ != newArray.length? ", " : "") );
        }
        System.out.println(" ]");
    }

    public static int[][] replicate(int[][] array)
    {
        int x = array.length;
        int y = array[0].length;

        int counterX = 0;
        int counterY = 0;

        int[][] newArray = new int[2 * x][2 * y];
        for(int[] a : array)
        {
            for(int b : a)
            {
                newArray[counterX++][counterY] = b;
                newArray[counterX--][counterY++] = b;

                newArray[counterX++][counterY] = b;
                newArray[counterX--][counterY++] = b;
            }
            counterY = 0;
            counterX++;
            counterX++;
        }

        return newArray;
    }
}

output:

[ [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1] ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文