Java Arrays.copyOfRange Examples

Java Arrays.copyOfRange - 30 examples found. These are the top rated real world Java examples of java.util.Arrays.copyOfRange extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public static TreeNode buildTree(int[] preorder, int[] inorder) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    if (inorder.length == 0) return null;
    int value = preorder[0];
    TreeNode root = new TreeNode(value);
    int pos = 0;
    while (inorder[pos] != value) {
      pos++;
    }

    TreeNode left;
    TreeNode right;
    int[] emp = {};
    if (pos == 0) left = buildTree(emp, emp);
    else
      left =
          buildTree(Arrays.copyOfRange(preorder, 1, pos + 1), Arrays.copyOfRange(inorder, 0, pos));

    if (pos == inorder.length - 1) right = buildTree(emp, emp);
    else
      right =
          buildTree(
              Arrays.copyOfRange(preorder, pos + 1, preorder.length),
              Arrays.copyOfRange(inorder, pos + 1, inorder.length));

    root.left = left;
    root.right = right;
    return root;
  }
Example #2
0
  /**
   * Creates a new {@code TX64Packet} object from the given payload.
   *
   * @param payload The API frame payload. It must start with the frame type corresponding to a TX64
   *     Request packet ({@code 0x00}). The byte array must be in {@code OperatingMode.API} mode.
   * @return Parsed TX (transmit) 64 Request packet.
   * @throws IllegalArgumentException if {@code payload[0] != APIFrameType.TX_64.getValue()} or if
   *     {@code payload.length < }{@value #MIN_API_PAYLOAD_LENGTH} or if {@code frameID < 0} or if
   *     {@code frameID > 255} or if {@code transmitOptions < 0} or if {@code transmitOptions >
   *     255}.
   * @throws NullPointerException if {@code payload == null}.
   */
  public static TX64Packet createPacket(byte[] payload) {
    if (payload == null)
      throw new NullPointerException("TX64 Request packet payload cannot be null.");

    // 1 (Frame type) + 1 (frame ID) + 8 (address) + 1 (transmit options)
    if (payload.length < MIN_API_PAYLOAD_LENGTH)
      throw new IllegalArgumentException("Incomplete TX64 Request packet.");

    if ((payload[0] & 0xFF) != APIFrameType.TX_64.getValue())
      throw new IllegalArgumentException("Payload is not a TX64 Request packet.");

    // payload[0] is the frame type.
    int index = 1;

    // Frame ID byte.
    int frameID = payload[index] & 0xFF;
    index = index + 1;

    // 8 bytes of address, starting at 2nd byte.
    XBee64BitAddress destAddress64 =
        new XBee64BitAddress(Arrays.copyOfRange(payload, index, index + 8));
    index = index + 8;

    // Transmit options byte.
    int transmitOptions = payload[index] & 0xFF;
    index = index + 1;

    // Get data.
    byte[] data = null;
    if (index < payload.length) data = Arrays.copyOfRange(payload, index, payload.length);

    return new TX64Packet(frameID, destAddress64, transmitOptions, data);
  }
Example #3
0
  public synchronized List<Transaction> getTransactions() {
    if (this.transactions == null) {
      // LOAD TRANSACTIONS
      this.transactions = new ArrayList<Transaction>();

      try {
        int position = 0;
        for (int i = 0; i < transactionCount; i++) {
          // GET TRANSACTION SIZE
          byte[] transactionLengthBytes =
              Arrays.copyOfRange(
                  this.rawTransactions, position, position + TRANSACTION_SIZE_LENGTH);
          int transactionLength = Ints.fromByteArray(transactionLengthBytes);

          // PARSE TRANSACTION
          byte[] transactionBytes =
              Arrays.copyOfRange(
                  this.rawTransactions,
                  position + TRANSACTION_SIZE_LENGTH,
                  position + TRANSACTION_SIZE_LENGTH + transactionLength);
          Transaction transaction = TransactionFactory.getInstance().parse(transactionBytes);

          // ADD TO TRANSACTIONS
          this.transactions.add(transaction);

          // ADD TO POSITION
          position += TRANSACTION_SIZE_LENGTH + transactionLength;
        }
      } catch (Exception e) {
        // FAILED TO LOAD TRANSACTIONS
      }
    }

    return this.transactions;
  }
  @Test
  public void testRemoveNode() throws Exception {
    waitForFullMesh(2000);
    IStoreClient<String, String> client0 =
        syncManagers[0].getStoreClient("global", String.class, String.class);
    IStoreClient<String, String> client1 =
        syncManagers[1].getStoreClient("global", String.class, String.class);
    IStoreClient<String, String> client2 =
        syncManagers[2].getStoreClient("global", String.class, String.class);

    client0.put("key", "value");
    waitForValue(client1, "key", "value", 2000, "client1");

    nodes.remove(0);
    nodeString = mapper.writeValueAsString(nodes);

    SyncManager oldNode = syncManagers[0];
    syncManagers = Arrays.copyOfRange(syncManagers, 1, 4);
    moduleContexts = Arrays.copyOfRange(moduleContexts, 1, 4);

    try {
      for (int i = 0; i < syncManagers.length; i++) {
        moduleContexts[i].addConfigParam(syncManagers[i], "nodes", nodeString);
        syncManagers[i].doUpdateConfiguration();
        waitForConnection(syncManagers[i], (short) 1, false, 2000);
      }
    } finally {
      oldNode.shutdown();
    }
    waitForFullMesh(2000);

    client1.put("newkey", "newvalue");
    waitForValue(client2, "key", "value", 2000, "client4");
    waitForValue(client2, "newkey", "newvalue", 2000, "client0");
  }
Example #5
0
  public boolean evaluate(byte[] event) {
    /*Selecciona del arreglo el byte del tipo de item*/

    byte itemType = event[0];

    if (itemType == SMOKETYPE) {
      /*Selecciona del arreglo el byte relacionado al status*/
      return smokeProcess(event[5]);

    } else if (itemType == SENSORTYPE) {
      /*Procesar evento*/
      byte[] codebytes = Arrays.copyOfRange(event, 1, 5);
      int code = Util.convertArraytoInt(codebytes);
      return sensorProcess(event[5], code);
    } else if (itemType == RFIDTYPE) {
      /*Procesar evento*/
      // System.out.println("RFID");
      byte[] codebytes = Arrays.copyOfRange(event, 1, 5);
      int code = Util.convertArraytoInt(codebytes);
      return sensorProcess(event[5], code);
    } else {
      /*No contiene un tipo de item definido*/
      return false;
    }
  }
Example #6
0
  private static BinarytreeNode ReCreateTree(int[] preOrder, int[] middleOrder) {
    if (preOrder == null || middleOrder == null) return null;
    if ((preOrder.length <= 0 && preOrder.length <= 0) || (preOrder.length != middleOrder.length))
      return null;
    BinarytreeNode root = new BinarytreeNode();
    root.value = preOrder[0];
    int middle = 0;
    for (int i = 0; i < middleOrder.length; i++) {
      if (middleOrder[i] == preOrder[0]) {
        middle = i;
      }
    }
    if (middle >= 0) { // 这里一定要是等于0也可以
      /** 每次都这样暴力的穿件一份数组其实很浪费 可以用指针移动来处理 */
      root.left =
          ReCreateTree(
              Arrays.copyOfRange(preOrder, 1, middle + 1),
              Arrays.copyOfRange(middleOrder, 0, middle));

      root.right =
          ReCreateTree(
              Arrays.copyOfRange(preOrder, middle + 1, preOrder.length),
              Arrays.copyOfRange(middleOrder, middle + 1, middleOrder.length));
    }
    return root;
  }