我们有定时采集数据的时间流,但在某些时候没有采集数据,因此我们在此时将QNAN放入数组中.唯一的问题是,每当我们对数据进行任何统计时,每次我们访问某个位置时都要检查它是不是NAN.所以我们的平均惯例看起来像
int n = 0;
double sum = 0;
for(int i = 0; i < SIZE; i++)
{
if(data[i] == data[i])
{
sum += data[i];
n++;
}
}
Run Code Online (Sandbox Code Playgroud)
我们可能总是将不得不算楠的,但将是很好,如果有一个跟你说,如果你添加一个无效的值设置为有效数量,有效数量保持不变.
我不确定是否有更好的方法,但想检查一下.谢谢,詹姆斯
我有两个相应的列表:
public class BookOverallData {
private Long idOfBook;
private String title;
private String authour;
private BigDecimal basePrice;
private Integer discountRate;
}
public class TimeDiscount {
private Long idOfBook;
private Integer discountRate;
}
Set<BookOverallData> booksToReturn
Set<TimeDiscount> actualPromotions
Run Code Online (Sandbox Code Playgroud)
我们的目标是要总结的折扣,这意味着将discountRate来自actualPromotions于discountRate价值从booksToReturn列表中.两个列表中的对象可以匹配idOfBook.
这就是我解决它的方式
booksToReturn.forEach(
p -> {
final Optional<TimeDiscount> promotion = actualPromotions.stream().filter(ap -> Objects.equals(ap.getIdOfBook(), p.getIdOfBook())).findFirst();
promotion.ifPresent(ap -> p.setDiscountRate(ap.getDiscountRate() + p.getDiscountRate()));
}
);
Run Code Online (Sandbox Code Playgroud)
我只是在探索溪流,我认为我的解决方案是块状的.如何使用流和功能方法以更优雅的方式解决这个问题?
我想创建一个模板化函数,它对const和非const数据的工作方式相同,只是它会根据需要返回一个const或非const指针.
例如,我想返回一个指向容器中匹配元素的指针:
template <class Container, class Pred>
typename Container::value_type* getValuePtrIf(Container& c, Pred pred)
{
auto it=std::find_if(c.begin(), c.end(), pred);
return (it!=c.end()) ? &(*it) : nullptr;
}
Run Code Online (Sandbox Code Playgroud)
但是我无法为const和非const调用构建它.如果我省略了Container& c声明中的const 然后它不能返回一个const指针,但如果我改为const Container& c那么我可以返回一个const指针,但是然后非const的情况不会构建.
有没有一种方法来定义它,以便将其const解释为Container模板参数的一部分,以便我只需要定义此函数的一个版本?
对于给定类型的Map,是否有任何保证迭代由返回的Collection视图keySet,values并且entries方法以相同的顺序迭代?
背景:我想知道是否转型
public static void doSomethingForEachEntry(Map<String, Integer> someMap) {
for (String key : someMap.keySet()) {
doSomething(someMap.get(key));
}
}
Run Code Online (Sandbox Code Playgroud)
至
public static void doSomethingForEachEntry(Map<String, Integer> someMap) {
for (Integer value : someMap.values()) {
doSomething(value);
}
}
Run Code Online (Sandbox Code Playgroud)
保证迭代顺序不变.
我有一个计算机科学课的项目,我们正在制造战舰.该计划的一部分是我们确保玩家放下的棋子不会脱离棋盘.
我已经做了一个方法来检查它是否脱离了董事会:
private static boolean test(String s, int row, int column,int spaces)
{
if(s.equals("right")&&column+5<=10)
{
return true;
}
if(s.equals("up")&&row-spaces>=0)
{
return true;
}
if(s.equals("left")&&column-spaces>=0)
{
return true;
}
if(s.equals("Down")&&row+spaces<=10)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但是,一旦我得到它打印出一条错误信息,我不知道如何制作它以便程序可以重新接收该片段的新位置,而不在if和if语句中放入if语句声明(以及开启和关闭),因为您需要检查新位置以确保它不会脱离电路板.
这是我获得比赛位置的部分(虽然我认为你不需要它)
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt(); …Run Code Online (Sandbox Code Playgroud) BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Run Code Online (Sandbox Code Playgroud)
我只想知道在使用上述语句时我可以输入多少个字符.例如,如果我可以将"aaaaa"或"abcde"放入控制台,那就意味着我可以放入5个或更多字符.那我可以输入"a"2,147,483,647次吗?(整数的最大值)System.in似乎在内部存储输入序列作为整数类型.
如果是,我可以输入多少个2字节字符?是1,073,741,823吗?(这个数字的一半)
在下面的代码中,该行System.out.println(sumInteger(bigs) == sumInteger(bigs));显示为false.但是当我们再次比较另一个Integer包装类时System.out.println(bc == ab);,它返回true.为什么在第一种情况下包装类的比较为false而在第二种情况下为true?
import java.util.Arrays;
import java.util.List;
public class Arrays {
public void array1() {
List<Integer> bigs = Arrays.asList(100,200,300);
System.out.println(sumInteger(bigs) == sum(bigs)); // 1. Output: true
System.out.println(sumInteger(bigs) == sumInteger(bigs)); //2. Output: false
Integer ab = 10;
System.out.println(ab == 10); //3. Output: true
Integer bc = 10;
System.out.println(bc == ab); //4. Output: true
}
public static int sum (List<Integer> ints) {
int s = 0;
for (int n : ints) { s += n; } …Run Code Online (Sandbox Code Playgroud) 我最近遇到了这个代码,但不太明白发生了什么.
auto c = vector<int> {};
Run Code Online (Sandbox Code Playgroud)
返回的向量构造函数是什么?
然后这段代码:
c = vector<int> {1,2,3,4,5 };
Run Code Online (Sandbox Code Playgroud)
第二个c是与初始位置不同的内存位置c吗?
c重新初始化时是否会调用析构函数?
我搜索了互联网,但找不到上述代码的任何示例.
以上是如何不同的
vector<int> c {};
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
(true或false)在循环体执行时分配在循环体中声明的局部变量的空间,并在主体完成时释放.
这个问题的答案是错误的.但为什么?
我写了一些使用一些resteasy库的代码。
该代码在 Eclipse 中运行良好,但在使用 maven shade 插件作为 fat-jar 构建执行时会产生异常。
原因:在 META-INF/services/javax.ws.rs.ext.Providers 下创建的 jar 中,只列出了来自 resteasy-client 的提供者。然而,我还需要来自 resteasy-jackson2-provider 和来自 resteasy-jaxrs 的提供者。
我认为问题可能是,所有 3 个库(resteasy-client、resteasy-jackson2-provider、resteasy-jaxrs)都使用同名文件来列出它们的提供者(META-INF/services/javax.ws.rs.ext.供应商)。因此,也许 maven 会用其他库中的列表覆盖一个库中的提供者列表?
我的 pom.xml 看起来像这样:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.6.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.6.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>playarounds.ServiceMainClass</Main-Class>
</manifestEntries> …Run Code Online (Sandbox Code Playgroud) java ×7
c++ ×3
c++11 ×2
boxing ×1
collections ×1
const ×1
dictionary ×1
iteration ×1
java-8 ×1
java-stream ×1
loops ×1
maven ×1
resteasy ×1
variables ×1
vector ×1