简体   繁体   English

这个构造函数是什么样的?

[英]What will this constructor look like?

public byte[] d

In this byte array, each byte represents a digits, where d[0] is the least significant digit, and a[d.length-1] is the most significant digit. 在此字节数组中,每个字节代表一个数字,其中d [0]是最低有效数字,而a [d.length-1]是最高有效数字。 For example, 543210 is stored as {0,1,2,3,4,5}. 例如,543210被存储为{0,1,2,3,4,5}。 The most significant digit can't be a zero; 最高有效位数不能为零;

In the real implementation, this array should be private. 在实际的实现中,此数组应为私有。

Constructor Detail: AdditionOnlyInt 构造函数详细信息:AdditionOnlyInt

public AdditionOnlyInt(java.lang.String a)

This is a constructor that construct an AdditionOnlyInt with value presented by the input string. 这是一个构造函数,该构造函数使用输入字符串表示的值构造一个AdditionOnlyInt。 For example, if input a = "00012340", this constructor will set the byte array to have value {0,4,3,2,1}. 例如,如果输入a =“ 00012340”,则此构造函数将字节数组设置为值{0,4,3,2,1}。 Note that the leading zeros in the input string should not be stored. 请注意,不应存储输入字符串中的前导零。

Parameters:a - is a string such as "00012340"

I do not know how to do this constructor does anyone? 我不知道该做什么构造函数的人吗?

I know its very wrong but I tried this 我知道它很错,但我尝试过

public AdditionOnlyInt(String number)  {
   int counter = number.length();
   number.replace("0","");
   data = new byte[number.length()];
   int i = 0;
   while(i<number.length())  {
      data[i] = (byte)number.charAt(counter);
      i++;
      counter--;
   }
}

and I do know converting to byte gives you different values. 而且我知道转换为字节会为您提供不同的值。

You're in luck: only a handful of modifications need to be made to your program. 您很幸运:只需要对程序进行少量修改。 You weren't entirely wrong. 你并不完全错。 :) :)

First, these three statements are giving you fits: 首先,这三个陈述很适合您:

int counter = number.length();
number.replace("0","");
data = new byte[number.length()];

You get a counter, which is the unbiased String (that is, with zeroes), which will undoubtedly be smaller than the String, without zeroes. 您会得到一个计数器,它是无偏字符串(即带有零的字符串),无疑将小于不带零的字符串。 You'd also be creating the array with the size of the unbiased list. 您还将使用无偏列表的大小来创建数组。

Well...it would be if your second statement did something. 嗯......这是,如果你的第二个语句做了。 String is immutable, so anything that's done to modify it would only generate a new String , leaving the old one unmodified. String是不可变的,因此任何修改它的操作只会生成一个新的String ,而旧的则保持不变。 That's fixable by this: 这是可以解决的:

number = number.replace("0", "");

...but in reality, given your input set, it should be fixed by this: ...但是实际上,给定您的输入集, 通过以下方式对其进行修复:

number = String.valueOf(Integer.parseInt(number));

This way, you keep internal zeroes. 这样,您可以保留内部零。

Now assuming that your byte[] is actually called data and not d , there's one little issue we have to fix: numbers in terms of a byte are quite large (that is, the character for '7' is 0x37, which is 55). 现在假设您的byte[]实际上被称为data不是 d ,我们必须解决一个小问题:以byte为单位的数字非常大(也就是说,“ 7”的字符为0x37,即55) 。

So we need to bias it. 因此,我们需要对它进行偏见。 Whatever our byte number is, we need to subtract '0' from it; 无论我们的byte数是多少,我们都需要从中减去'0' that is, we need to subtract 48 from it, to give us our correct value. 也就是说,我们需要从中减去48,以得出正确的值。 I'll show you what that looks like in a moment. 一会儿,我会告诉你。

Now, for your loop: 现在,为您的循环:

int i = 0;
while(i<number.length())  {
   data[i] = (byte)number.charAt(counter);
   i++;
   counter--;
}

I'm not sold on the necessity of counter, so let's get rid of it. 我没有因为需要柜台而被出售,所以让我们摆脱它。 Now we'll use i from now on. 现在,我们将从现在开始使用i Essentially, what this means is that we have to move charAt from the end of the String to the front of the String , placing the values into the array as such. 本质上,这是什么意思是,我们必须移动charAt从的末端String到的前String ,将值放入该阵列本身。 What that (mostly) looks like is this: (大部分)看起来是这样的:

data[i] = (byte) (number.charAt(number.length() - 1 - i);

Pay close attention here - we have to subtract 1 from the length right off the bat, since we don't have a place on the String that's exactly equal to its maximum length. 在这里要特别注意 -我们必须立即从蝙蝠的长度中减去1,因为我们在String上没有一个与其最大长度完全相等的位置。 We then subtract i from that, so we get the effect of moving backwards on the String . 然后,我们从中减去i ,这样就得到了String上向后移动的效果。

That is, for a string of length 10 without zeroes, if we start at i = 0 , we get the character at 10 - 1 - 0 = 9 , or the last character. 也就是说,对于长度为10且不为零的字符串,如果我们从i = 0开始,则得到的字符为10 - 1 - 0 = 9或最后一个字符。

Remember what I said about biasing the result of that, though? 还记得我所说的偏见结果吗? After you've got the data, be sure to subtract '0' from it. 获得数据后,请确保从中减去'0'

data[i] = (byte) (number.charAt(number.length() - 1 - i) - '0';

And really, that's all there is to it. 确实,这就是全部。 You mostly got it, except the iteration and sanitization was a bit wonky. 除了迭代和清理有点笨拙外,您大部分都已得到它。

public AdditionOnlyInt(String input)
{
    //remove trailing 0s
    int  relevStart = 0;
    while(input.charAt(relevStart) == '0')
    {
        relevStart++;
    }
    String relevantTerms = input.substring(relevStart);
    //flip the remaining chars
    int length = relevantTerms.length();
    data = new byte[length];
    for(int iter = 0; iter < length; iter++)
    {
        data[iter] = (byte) (relevantTerms.charAt(length - iter - 1) - '0');
    }
}

Hope this helps. 希望这可以帮助。

Step 1: Check whether its a number by using Integer value = Integer.valueOf(args); 步骤1:使用Integer value = Integer.valueOf(args);检查其是否为数字Integer value = Integer.valueOf(args);

Step 2: convert the Integer into a byte array by calling byte array[] = value.toString().getBytes(); 步骤2:通过调用byte array[] = value.toString().getBytes();将Integer转换为字节数组byte array[] = value.toString().getBytes();

Step 3: the byte array contains the value in forward fashion, So swapping all the values in the array will make the digit reverse as user requested. 步骤3:字节数组以正向方式包含该值,因此交换该数组中的所有值将按用户要求将数字反转。

for (int i = 0, j = array.length - 1; i < array.length / 2; i++, j--) {
                        byte temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }

find the complete program below: 在下面找到完整的程序:

   public class AdditionOnlyInt {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new AdditionOnlyInt("01010120");

        }

        public AdditionOnlyInt(String args) {
            try {
                Integer value = Integer.valueOf(args);

                byte array[] = value.toString().getBytes();

                for (int i = 0, j = array.length - 1; i < array.length / 2; i++, j--) {
                    byte temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }

                for (int i = 0; i < array.length; i++) {
                    System.out.print((char) array[i]);
                }
            } catch (Exception e) {
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM