小编Thi*_*ruG的帖子

使用linq查询加入两个列表 - C#

我有一个任务,我必须加入两个相同类型的列表(客户).他们有类似的条目,我必须避免重复.

这是我的客户类:

class Customer
{
  private String _fName, _lName;
  private int _age, _cusIndex;
  private float _expenses;

  public Customer(String fName, String lName, int age, float expenses, int cusIndex)
  {
    this._fName = fName;
    this._lName = lName;
    this._age = age;
    this._expenses = expenses;
    this._cusIndex = cusIndex;
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我有两个List<Customer>名字customers1customers2.我需要在不使用Collections方法的情况下加入这两个方法(比如customer1.Union(customer2).ToList();使用Linq查询).

这是我写的Linq查询:

var joined = (from c1 in customers1
              join c2 in customers2
              on c1.CusIndex equals c2.CusIndex
              select new {c1, c2});
Run Code Online (Sandbox Code Playgroud)

但是这给了我出现在两个列表上的成员.但我需要所有人,而不是重复.有什么办法吗?

c# linq join list linq-query-syntax

2
推荐指数
1
解决办法
1万
查看次数

将字符串转换为芭蕾舞女演员的Json

有没有办法转换stringjson芭蕾舞女演员?

我发现这个PR - 添加jsons:parse()方法从一个字符串获取一个JSON,它表示添加支持解析stringjson,但找不到任何示例.

我尝试了以下方法:

string person = {"name":"John", "address":{"number":89, "street":"main street", "town": "Colombo"}};
json personJson = sons:parse(person);
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误:

undefined package 'jsons'
undefined function 'parse'
Run Code Online (Sandbox Code Playgroud)

string json type-conversion ballerina

2
推荐指数
1
解决办法
235
查看次数

从特定主题中检索 Kafka 消费者的最后 n 条消息

卡夫卡版本:0.9.0.1

如果n = 20,我必须获取某个主题的最后 20 条消息。

我试过

kafkaConsumer.seekToBeginning();
Run Code Online (Sandbox Code Playgroud)

但它检索所有消息。我只需要获取最后 20 条消息。

这个话题可能有几十万条记录

public List<JSONObject> consumeMessages(String kafkaTopicName) {
  KafkaConsumer<String, String> kafkaConsumer = null;
  boolean flag = true;
  List<JSONObject> messagesFromKafka = new ArrayList<>();
  int recordCount = 0;
  int i = 0;
  int maxMessagesToReturn = 20;

  Properties props = new Properties();         
  props.put("bootstrap.servers", "localhost:9092");
  props.put("group.id", "project.group.id");
  props.put("max.partition.fetch.bytes", "1048576000");
  props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
  props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
  kafkaConsumer = new KafkaConsumer<>(props);

  kafkaConsumer.subscribe(Arrays.asList(kafkaTopicName));
  TopicPartition topicPartition = new TopicPartition(kafkaTopicName, 0);
  LOGGER.info("Subscribed to topic " + kafkaConsumer.listTopics());
  while (flag) { …
Run Code Online (Sandbox Code Playgroud)

java apache-kafka kafka-consumer-api kafka-records

2
推荐指数
1
解决办法
1万
查看次数

如何从具有特定偏移量的 kafka 主题获取消息

我们有一个带有 3 个 kafka 代理的 HDP 集群(来自 hortonworks)

我们想运行 kafka 控制台消费者,以便从具有特定偏移量的主题中获取一条消息

/usr/hdp/current/kafka-broker/bin/kafka-console-consumer.sh --zookeeper zoo01:2181  --topic lopet.lo.pm--partition 0 --offset 34537263 --max-messages 1
Run Code Online (Sandbox Code Playgroud)

但我们得到以下信息:

我们错在哪里?

Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
Partition-offset based consumption is supported in the new consumer only.
Option                                   Description
------                                   -----------
--blacklist <blacklist>                  Blacklist of topics to exclude from
                                           consumption.
--bootstrap-server <server to connect    REQUIRED (unless …
Run Code Online (Sandbox Code Playgroud)

apache-kafka kafka-consumer-api hdp

2
推荐指数
1
解决办法
7849
查看次数

字符串获取错误的值

我正在尝试构建一个字符串,11 11但我面临的问题是我得到start以下字符串,98 11而不是11 11.

我该如何解决这个问题?

我感谢任何帮助.

Character number = newName.charAt(2); //here number is 1
Character numberBefore = newName.charAt(1); //here numberBefore is 1

try (PrintWriter writer = new PrintWriter(path+File.separator+newName);
    Scanner scanner = new Scanner(file)) {
  boolean shouldPrint = false;
  while (scanner.hasNextLine()) {
  String line = scanner.nextLine();

  if(numberBefore >0 ){
    String start= number+number+" "+number+number; //here start is `98 11`
  }
Run Code Online (Sandbox Code Playgroud)

java string stringbuilder

1
推荐指数
1
解决办法
143
查看次数

从 ARM 汇编到 C 的函数调用的参数传递约定

我有一个 C 代码,它调用 ARM Assembly 中定义的函数。必须传递两个参数。

如果函数调用如下所示:

functionName(a, b)
Run Code Online (Sandbox Code Playgroud)

寄存器x0并按x1什么顺序保存这些值?是x0一直持有a还是一直x1持有b,还是相反?

c assembly arm parameter-passing

1
推荐指数
1
解决办法
4237
查看次数

JAVA TCP 客户端-服务器连接

当我运行这个程序时,Client 类会提示用户输入一个命令,该命令应该转到 Server 类并打开一个文件并读取该文件的每一行并将字节长度返回给 Client 类以进行显示。

不幸的是,一旦我输入命令,就没有任何反应并且不知道为什么。

TCP客户端类

package TcpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.io.*;

public class TcpClient {
  public static void main(String[] args) {
    String temp;
    String displayBytes;
    try {
      //create input stream
      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
      //create client socket, connect to server
      Socket clientSocket = new Socket("localhost",5555);
      //create output stream attached to socket
      DataOutputStream outToServer =
      new DataOutputStream(clientSocket.getOutputStream());

      System.out.print("Command : ");

      //create input stream attached to socket
      BufferedReader inFromServer =
      new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); …
Run Code Online (Sandbox Code Playgroud)

java tcpclient tcpserver

1
推荐指数
1
解决办法
2万
查看次数

变量具有初始化程序但不完整的类型C++

我是C++的新手.我有这个代码来创建一个结构,以显示mutableC++ 中关键字的用法.

#include <iostream>

using namespace std;

int main()
{
  struct
  {
    mutable double radius;
    double PI = 3.142;

    double getArea()
    {
      return (PI * radius * radius);
    }
  } circle;

  const struct circle c1 = {2.0};
  circle.radius = 7;

  cout << "Area is " << circle.getArea() << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是在编译时我收到以下错误消息:

error: variable const main()::circle c1 has initializer but incomplete type
Run Code Online (Sandbox Code Playgroud)

错误在于c1该行const struct circle c1 = {2.0};.任何人都可以在这里指出我的错误.

c++ struct compiler-errors

1
推荐指数
2
解决办法
3132
查看次数

将 byte[] 作为单个参数传递给可变参数方法

我有一个方法,它有一个 varargs 参数。它看起来像这样:

public void sampleFunction(String name, Object... args) {

}
Run Code Online (Sandbox Code Playgroud)

我想将 a 传递byte[]给此方法,但作为单个参数。我怎样才能做到这一点?

byte[] myByteArray = functionReturningAByteArray();

sampleFunction("Name", myByteArray);
Run Code Online (Sandbox Code Playgroud)

这会被视为一个数组,还是会myByteArray被视为一个单一的对象?

当我通过时byte[],IntelliJ 将其报告为Confusing primitive array argument to varargs method. 我试图按照它的建议转换myByteArray为 an Object,然后它报告它为多余的转换。

我想将它作为单个对象传递。

java arrays variadic-functions

1
推荐指数
1
解决办法
280
查看次数

在 OCaml 中查找整数的符号

我们可以在不使用二元运算的情况下在 OCaml 中找到 inter 的符号吗?我的意思是很容易通过与 0 进行比较来获得符号。除了使用 match with command 之外,还有其他方法吗?我尝试了以下

let sign n =
    let k = abs(n) in
    match k with
    | 0 -> 0
    | n -> 1
    | _ -> (-1);;
Run Code Online (Sandbox Code Playgroud)

但它不适用于负数,因为它表明最后一次比较未使用。:/

ocaml sign

0
推荐指数
1
解决办法
598
查看次数

Java - 检查字符串是否包含相同的数字

我有一个int例子:

151515
1
1111111
4848484
111
888
Run Code Online (Sandbox Code Playgroud)

我需要测试/检查数字是否仅包含1,有效示例:

1
11111
1111111111
11
Run Code Online (Sandbox Code Playgroud)

示例无效:

88888888888
8885555
4747
7
889
Run Code Online (Sandbox Code Playgroud)

有什么建议?可能是任何regex?或者有一个没有正则表达式的快速解决方案,并将值保留为int而不将其解析为String.

java regex string validation

-4
推荐指数
1
解决办法
920
查看次数

parse int中的数字格式异常

public static void main(String[] args)throws IOException {
  String s ="12312a";
  int x = Integer.parseInt(s);
  System.out.println (x+2);
}
Run Code Online (Sandbox Code Playgroud)

而我所拥有的只是:

Exception in thread "main" java.lang.NumberFormatException: For input string: "12312a"

任何提示?

java parseint numberformatexception

-6
推荐指数
1
解决办法
121
查看次数