我试图找到以下模式:
不知道可能发生的任何模式.
例如:
'ell', 'the b', 'y '
.'the boy fell by the bell'
.使用双for循环,它可以非常低效地强制使用:
ArrayList<String> patternsList = new ArrayList<>();
int length = string.length();
for (int i = 0; i < length; i++) {
int limit = (length - i) / 2;
for (int j = limit; j >= 1; j--) {
int candidateEndIndex = i + j;
String candidate = string.substring(i, candidateEndIndex);
if(candidate.length() <= 1) {
continue;
}
if (string.substring(candidateEndIndex).contains(candidate)) …
Run Code Online (Sandbox Code Playgroud) 我有一个工作簿,其中包含一些数据.我正在使用该工作簿并根据其他工作簿中的数据创建另一个带有折线图的工作簿.代码运行正常,但每当我打开图形文件时,我都会收到警告We can't update some of the links in your workbook right now
.如果单击Edit Links...
警告菜单中的按钮,则表明无法找到数据工作簿.如果我单击Change Source...
,并选择正确的工作簿,它就可以正常工作.为什么是这样?POI可以不保留两个文件之间的链接吗?
我的代码:
要创建数据工作簿:
public static XSSFWorkbook createDataSpreadsheet(String name, long[] data) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(name);
int rowNumber = 0;
for(int i = 1; i < data.length + 1; i++) {
Row row = sheet.createRow(rowNumber++);
int columnNumber = 0;
row.createCell(columnNumber++).setCellValue(i);
row.createCell(columnNumber++).setCellValue(data[i - 1]);
}
return workbook;
}
Run Code Online (Sandbox Code Playgroud)
要创建图表工作簿:
public static XSSFWorkbook createLineChart(String name, XSSFWorkbook data) {
XSSFWorkbook …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将2D列表转换为2D int
数组.但是,似乎我只能收集对象,而不是基元.
当我做:
data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new);
Run Code Online (Sandbox Code Playgroud)
我得到编译时错误Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
.
但是,如果我int[]
改为Integer[]
,则编译.我如何才能使用它int
?
假设我有一个原型如下所示的函数,属于类container_class
:
std::vector<int> container_class::func(int param);
Run Code Online (Sandbox Code Playgroud)
该函数可能会或可能不会在某些输入上产生无限循环; 无法确定哪些输入会导致成功,哪些输入会导致无限循环.该函数在一个库中,我没有源代码,也无法修改(这是一个bug,将在几个月内在下一个版本中修复,但是现在我需要一种方法来解决它),所以修改函数或类的解决方案将不起作用.
我尝试使用std::async
和隔离函数std::future
,并使用while循环来不断检查线程的状态:
container_class c();
long start = get_current_time(); //get the current time in ms
auto future = std::async(&container_class::func, &c, 2);
while(future.wait_for(0ms) != std::future_status::ready) {
if(get_current_time() - start > 1000) {
//forcibly terminate future
}
sleep(2);
}
Run Code Online (Sandbox Code Playgroud)
这段代码有很多问题.一个是我无法强制终止std::future
对象(以及它所代表的线程).
在极端情况下,如果我找不到任何其他解决方案,我可以在自己的可执行文件中隔离该函数,运行它,然后检查其状态并适当地终止它.但是,我宁愿不这样做.
我怎么能做到这一点?有没有比我现在做的更好的方式?
我试过这个代码(list
是ArrayList<List<Integer>>
):
list.stream().flatMap(Stream::of).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但它没有做任何事情; 该列表仍然是2D列表.如何将此2D列表转换为1D列表?
我试过这个 StackOverflow答案的代码,但是我得到了错误Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
:
//data is int[][]
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toList()))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我已经将2D int数组转换为Stream:
IntStream dataStream = Arrays.stream(data).flatMapToInt(x -> Arrays.stream(x));
Run Code Online (Sandbox Code Playgroud)
现在,我想按升序对列表进行排序.我试过这个:
dataStream.sorted().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑,因为在我看到的例子中,类似的事情没有错误.
作为编码的初学者,我正在为我的C考试做准备并正在做一个练习,我必须编写一个程序,应该计算sinh x ...当我以正常的方式写它时它正常工作并计算出正确的答案,但是当我将我的函数从Call by value更改为Call by reference,我得到一个错误"无法将参数1从double转换为float"...为什么我得到它,当我使用该值时我应该考虑什么?在这里你可以看到我的代码:)
#include "stdafx.h"
#include "math.h"
#include "iostream"
float e=2.71828;
using namespace std;
float sinhyp (float *x)
{
return (0.5*(pow(e,*x)-pow(e,(-1)*(*x))));
}
int _tmain(void)
{
float erg=sinhyp(15.0);
printf("%f \n", erg);
system ("Pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的建议/帮助;)
当我将鼠标悬停在它们上面时,我想要突出显示16个Jpanel.我匿名创建了JPanels,然后将它们添加到父级,并为每个JPanel添加了一个MouseListener.然后我将一个MouseListener添加到父级.问题是,现在它只突出了父母.我怎样才能解决这个问题?
注意:有时JFrame不会显示任何内容 - 您只需要继续运行它(通常需要2-3次尝试).评论是否在> 5次尝试后仍然无效.
HighlightJPanels(创建JFrame,容器和子项,并添加MouseListeners)
public class HighlightJPanels extends JFrame{
private static final long serialVersionUID = 7163215339973706671L;
private static final Dimension containerSize = new Dimension(640, 477);
private JLayeredPane layeredPane;
static JPanel container;
public HighlightJPanels() {
super("Highlight Test");
setSize(640, 477);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(containerSize);
getContentPane().add(layeredPane);
createContainer();
layeredPane.add(container, JLayeredPane.DEFAULT_LAYER);
createChildren(4, 4);
container.addMouseMotionListener(new HighlightJPanelsContainerMouseListener());
}
private void createChildren(int columns, int rows){
for (int i = 0; i < columns; i++){
for (int j = 0; …
Run Code Online (Sandbox Code Playgroud) java ×7
java-stream ×4
c++ ×2
java-8 ×2
algorithm ×1
apache-poi ×1
c++11 ×1
charts ×1
excel ×1
linux ×1
list ×1
mouseevent ×1
mouseover ×1
primitive ×1
sorting ×1
std-future ×1
substring ×1