我有一个List<Users>.我想用特定的用户名获取流中(第一个)用户的索引.我不想竟要求User将.equals()一些描述User,只是为了有相同的用户名.
我可以想到这样做的丑陋方法(迭代和计数),但感觉应该有一个很好的方法来做到这一点,可能是通过使用Streams.到目前为止,我所拥有的最好的是:
int index = users.stream()
.map(user -> user.getName())
.collect(Collectors.toList())
.indexOf(username);
Run Code Online (Sandbox Code Playgroud)
这不是我写过的最糟糕的代码,但它并不好.它也不是那么灵活,因为它依赖于一个带有.equals()描述你正在寻找的属性的函数的类型的映射函数; 我宁愿有一些可以随心所欲的东西Function<T, Boolean>
谁知道怎么样?
这只是我自己想出来的东西,但它似乎是一个有趣的问题而且让我难过.
在二维空间中有一组点,其中一个点指定为"开始",一个指定为"结束".每个点都有坐标(以距离原点为单位),但也有"加速度数"(以米 - 秒为单位的Δ-V).到达某个点(包括开始点)后,您可以在任何方向上加速到该点的加速度数.边缘成本取决于您当前的速度,但您还必须朝着正确的方向前进.
是否有一种有效的算法来寻找到达终点的最快路径?我没有提出比"尝试每条路径并检查结果"更好的东西.Djikstra和其他简单的算法不起作用,因为你不能轻易地说中间点的一条路径比另一条路径更好或更差,如果它们以不同的初始速度到达.
如果这太简单了,如果你添加了必须在终点停止的要求怎么办?(即,当你到达终点时,你必须小于它的加速度值.)
编辑:要明确,方向很重要.在遍历图形时保持速度矢量,加速意味着向其添加矢量,其大小以该点的加速度数量为上限.这意味着在哪里建造了一个巨大的速度是有害的,因为你会走得太快,以"引导"对其他宝贵的意见/目的地的情况.
这是一个非常小的问题,我知道如何禁用该警告,但阅读它后我怀疑它可能表明我可能对我的宏做了一些不正确的事情。无论如何,我有一个Rational有理数结构:
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Rational {
n: i128,
d: i128,
}
impl Rational {
pub fn new(n: i128, d: i128) -> Self {
Rational { n, d }
}
}
Run Code Online (Sandbox Code Playgroud)
为了以可读的方式创建它们,我使用宏:
macro_rules! rat {
($whole: tt $n : tt / $d : tt) => {
crate::rational::Rational::new($n + $whole * $d, $d)
};
($n : tt / $d : tt) => {
crate::rational::Rational::new($n, $d)
};
($n : tt) => {
crate::rational::Rational::new($n, 1)
};
} …Run Code Online (Sandbox Code Playgroud) use std::ops::Add;
#[derive(Debug)]
struct Sample {
pub value: usize,
}
impl std::ops::Add<&Sample> for &Sample {
type Output = Sample;
fn add(self, other: &Sample) -> Sample {
Sample {
value: self.value + other.value,
}
}
}
fn main() {
let mut a = Sample { value: 0 };
let b = Sample { value: 1 };
let mut_a = &mut a;
let immut_b = &b;
println!("Works: {:?}", mut_a.add(immut_b));
println!("And this works: {:?}", immut_b.add(mut_a));
println!("Also works:: {:?}", immut_b + mut_a);
println!("Still works:: …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的 IR 解释器。我知道如何使用 IR 和返回类型的枚举来执行此操作,但有几个原因我不想这样做:
impl,当 an 中的所有内容都enum属于同一类型时,这会受到限制因此,我非常希望摆脱由特征表示的 IR 和返回数据,例如:
trait Data{}
trait IR{
fn evaluate(self) -> Box<dyn Data>;
}
Run Code Online (Sandbox Code Playgroud)
但我在尝试进行评估时遇到了障碍:
struct Bool(bool);
impl Data for Bool{}
//other things impl Data, of course
struct IfThenElse<T1, T2, T3>{
condition: T1,
then_branch: T2,
else_branch: T3
}
impl<T1, T2, T3> IR for IfThenElse<T1, T2, T3> where T1: IR, T2: IR, T3: IR{
fn evaluate(self) -> Box<dyn Data>{ …Run Code Online (Sandbox Code Playgroud) 我试图将 JTable 显示为网格,单元格之间有线条。不过,我只能在单个单元格中添加边框,这看起来永远不正确;如果我添加完整的边框,我会得到一堆断开的框,看起来很丑陋和错误。使用 MatteBorders(如下面的代码)看起来会好一些,但会导致边界线不完全相交的间隙。
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component stamp = super.prepareRenderer(renderer, row, column);
int top = 1;
int left = 1;
int bottom = row == 7 ? 1 : 0; //Grid is always 8x8, this ensures the bottom/right will have full borders.
int right = column == 7 ? 1 : 0;
MatteBorder border = new MatteBorder(top, left, bottom, right, Color.BLACK);
if (stamp instanceof JComponent) {
((JComponent) stamp).setBorder(border);
}
return stamp;
}
Run Code Online (Sandbox Code Playgroud)
我觉得必须有某种方法可以正确地做到这一点,这样我才能在单元格元素之间获得网格线。我错过了什么?如果不出意外,有没有办法让 …
所以我读过为什么我不能在同一结构中存储值和对该值的引用?我明白为什么我的天真方法不起作用,但我仍然很不清楚如何更好地处理我的情况。
我有一个程序,我想构造如下(省略细节,因为无论如何我都无法编译它):
use std::sync::Mutex;
struct Species{
index : usize,
population : Mutex<usize>
}
struct Simulation<'a>{
species : Vec<Species>,
grid : Vec<&'a Species>
}
impl<'a> Simulation<'a>{
pub fn new() -> Self {...} //I don't think there's any way to implement this
pub fn run(&self) {...}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我创建一个向量Species(在 的生命周期内不会改变Simulation,除非在特定的互斥锁保护字段中),然后创建一个代表哪个物种生活在哪里的网格,它将自由改变。这个实现是行不通的,至少我想不出任何办法。据我了解,问题在于,无论我如何使用我的new方法,当它返回时,中的所有引用grid都会变得无效,因为Simulation,因此Simulation.species会被移动到堆栈中的另一个位置。即使我可以向编译器证明species及其内容将继续存在,它们实际上不会位于同一个位置。正确的?
我研究了解决这个问题的各种方法,例如在堆上制作或species使用Arcusize而不是引用,以及在物种向量中实现我自己的查找函数,但这些看起来更慢、更混乱或更糟糕。我开始思考的是,我需要真正重新构建我的代码,使其看起来像这样(详细信息用占位符填充,因为现在它实际运行):
use std::sync::Mutex;
struct Species{
index : usize,
population : Mutex<usize>
} …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
\n error[E0635]: unknown feature `proc_macro_span_shrink`\n --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.50/src/lib.rs:92:30\n |\n92 | feature(proc_macro_span, proc_macro_span_shrink)\n | ^^^^^^^^^^^^^^^^^^^^^^\n\nFor more information about this error, try `rustc --explain E0635`.\nerror: could not compile `proc-macro2` (lib) due to previous error\nRun Code Online (Sandbox Code Playgroud)\n我通过将我的 Rust 版本设置为特定的旧版夜间构建找到了解决方案,但这不适用于另一个项目,我真的想要一个更好的解决方案。我假设我正在使用某些东西的过时版本,或者也许我可以摆脱我的 Cargo.toml 文件中的依赖项,但该错误并没有告诉我有关哪个版本的任何信息。
\n经过一番搜索后,我尝试运行cargo tree -e features -i proc-macro2,但是虽然输出了很多涉及多个依赖项的内容,但没有任何具体涉及proc_macro_span_shrink.
proc-macro2 v1.0.50\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 quote v1.0.17\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 syn v1.0.90\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 proc-macro-error v1.0.4\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 proc-macro-error feature "default"\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 clap_derive v4.1.8 (proc-macro)\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 clap_derive feature "default"\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 clap v4.1.8\n\xe2\x94\x82 …Run Code Online (Sandbox Code Playgroud) rust ×5
java ×2
algorithm ×1
border ×1
graph-theory ×1
java-8 ×1
java-stream ×1
jtable ×1
macros ×1
math ×1
rust-cargo ×1
rust-macros ×1
swing ×1