从另一个数组创建一个数组? [英] Creating an array from another array?

查看:100
本文介绍了从另一个数组创建一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java的新手,并且正在使用RLE编码,我正在尝试创建一个程序,该程序采用一个字节数组,然后返回另一个字节数组,但它采用数组中连续值的数量,然后打印[(重复),((值)]。例如,[13,13,13,3,3,3,3]将返回[3,13,4,3]

New to java and am working with RLE encoding and I am trying to create a program that takes a byte array and then returns with another byte array but it takes the number of consecutive values in an array and then prints [ (number of times repeated),(value)]. Example, [ 13,13,13,3,3,3,3 ] would return [3,13,4,3]

import java.util.Arrays;

public class testing {

    public static void main(String [] args) {

        byte [] pracArray = {13,13,13,3,3,3,3};

        int count = 1;

        for (int i = 0; i <pracArray.length-1; i++)
        {
            if (pracArray[i] != pracArray[i+1])
            {
                count++;
            }
        }

        byte numLength = 1;
        byte indexNum = 0;
        int newArraySize = count*2;

        byte [] newArray = new byte [newArraySize];

        for ( int i = 0; i < pracArray.length-1; i++)
        {
            if (pracArray[i] != pracArray[i+1] )
            {
                newArray[indexNum] = numLength;
                newArray[indexNum+1] = pracArray[i];
                indexNum = (byte) (indexNum + 2);
                numLength = 1;
            }
            else
            {
                numLength++;
            }
        }
        System.out.println(Arrays.toString((pracArray)));
        System.out.println(Arrays.toString((newArray)));
    }
}

我知道我的问题,上一次连续的数字不会被打印。如果我将数组更改为[13,13,13,3,3,3,3,1],它将打印[3,13,4,3,0,0],最后的数字序列将不会被记录,因此newArray的最后两个值永远不会被填充。关于如何更改if语句以包含最后的数字序列的任何想法?

I am aware of my problem, the last run of consecutive numbers does not get printed. If I change the array to [13,13,13,3,3,3,3,1] it will print [3,13,4,3,0,0], the last sequence of numbers is never recorded so the last two values of newArray never get filled. Any ideas on how I can change my if statement to include my last sequence of numbers?

推荐答案

在循环终止时,检查最后一个元素是否等于倒数第二个元素。

On the termination of the loop, check if the last element is equal to the second last element.

您更新后的代码将是:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {

        byte[] pracArray = { 13, 13, 13, 3, 3, 3, 3, 1 };

        int count = 1;

        for (int i = 0; i < pracArray.length - 1; i++) {
            if (pracArray[i] != pracArray[i + 1]) {
                count++;
            }
        }

        byte numLength = 1;
        byte indexNum = 0;
        int newArraySize = count * 2;

        byte[] newArray = new byte[newArraySize];
        int i;
        for (i = 0; i < pracArray.length - 1; i++) {
            if (pracArray[i] != pracArray[i + 1]) {
                newArray[indexNum] = numLength;
                newArray[indexNum + 1] = pracArray[i];
                indexNum = (byte) (indexNum + 2);
                numLength = 1;
            } else {
                numLength++;
            }
        }
        if (pracArray[i - 1] == pracArray[i]) {
            newArray[indexNum] = numLength;
            newArray[indexNum + 1] = pracArray[i];
            indexNum = (byte) (indexNum + 2);
            numLength = 1;
        } else {
            newArray[indexNum] = numLength;
            newArray[indexNum + 1] = pracArray[i];
        }

        System.out.println(Arrays.toString((pracArray)));
        System.out.println(Arrays.toString((newArray)));
    }
}

输出:

[13, 13, 13, 3, 3, 3, 3, 1]
[3, 13, 4, 3, 1, 1]

byte []的输出[] pracArray = {13 、、 13、13、3、3、3、3} 如下:

[13, 13, 13, 3, 3, 3, 3]
[3, 13, 4, 3]

这篇关于从另一个数组创建一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆