在下面的代码中,为什么我不能从另一个线程中看到变量"i"?
public class Main {
public static void main(String[] args) {
int i = 0;
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(i);
}
}).start();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我可以在下面的代码中看到它?
public class Main {
int i = 0;
public static void main(String[] args) {
new Main().method();
}
private void method() {
new Thread(new Runnable() {
@Override
public void run() {
i = 1;
}
}).start();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个豆子
@Bean
public FilterRegistrationBean animalsFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new AnimalsFilter());
registration.addUrlPatterns(
"/api/cat",
"/api/cat/**",
"/api/dog"
);
...
return registration;
}
Run Code Online (Sandbox Code Playgroud)
在那个bean中,我使用了两种/api/cat**URL 模式.问题是,当我尝试使用复杂的postfix(/api/cat/1/feed)调用端点时,我的过滤器不会拦截请求.但是当我调用/api/cat和/api/got端点时它是可以的- 过滤器按预期工作并拦截请求.
如何为我的案例(/api/cat,/api/cat/**)使用多个URL模式?
PS
我试图使用下一个模式组合:
1) /api/cat, /api/cat**, /api/dog
2) /api/cat, /api/cat/**, /api/dog
3) /api/cat**, /api/dog
Run Code Online (Sandbox Code Playgroud) 如何更新/添加数组中的元素?
var persons = {
data: []
};
var bob = {name: 'Bob', age: 15};
var fill = {name: 'Fill', age: 20};
var mark = {name: 'Mark', age: 19};
var john = {name: 'John', age: 4};
persons['data'].push(bob);
persons['data'].push(fill);
persons['data'].push(mark);
persons['data'].push(john);
var updatedJohn = {name: 'John', age: 100};
if (!persons['data'][updatedJohn.name]){
persons['data'].push(updatedJohn);
} else {
persons['data'][updatedJohn.name] = updatedJohn; // this line doesn't work
}
Run Code Online (Sandbox Code Playgroud)
如果元素John已经存在,我无法弄清楚如何更新数组的元素.
UPDATE
如何获取父容器的所有后代元素?我想把它们放在阵列中.
<div class="parent">
<div class="child1">
<span class="child2">
<div class="child3">
<div class="child4">
<span class="child5"></span>
</div>
<div class="child6">
<div class="class7"></div>
</div>
</div>
</span>
<div class="child8"></div>
<span class="child9">
<div class="child10"></div>
</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我认为递归是一种选择.在第一点,我知道parent元素.
我需要按ID过滤问题。例如,我有#1,#2,#3,#4问题,并且我只需要显示第二和第三问题(#2,#3)。可能吗?
假设我有一个数组列group_ids
+-------+----------+
|user_id|group_ids |
+-------+----------+
|1 |[5, 8] |
|3 |[1, 2, 3] |
|2 |[1, 4] |
+-------+----------+
Run Code Online (Sandbox Code Playgroud)
架构:
root
|-- user_id: integer (nullable = false)
|-- group_ids: array (nullable = false)
| |-- element: integer (containsNull = false)
Run Code Online (Sandbox Code Playgroud)
我想获得所有对的组合:
+-------+------------------------+
|user_id|group_ids |
+-------+------------------------+
|1 |[[5, 8]] |
|3 |[[1, 2], [1, 3], [2, 3]]|
|2 |[[1, 4]] |
+-------+------------------------+
Run Code Online (Sandbox Code Playgroud)
到目前为止,我使用 UDF 创建了最简单的解决方案:
spark.udf.register("permutate", udf((xs: Seq[Int]) => xs.combinations(2).toSeq))
dataset.withColumn("group_ids", expr("permutate(group_ids)"))
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是通过 Spark 内置函数实现的东西。有没有办法在没有 UDF 的情况下实现相同的代码?
假设我有一个依赖于另一个bean的bean,另一个bean依赖于第一个bean.
Bean#1 -> Bean#2 -> Bean#1
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我想通过创建对象的副本Reflection API。这是我的代码:
private <T> T copy(T entity) throws IllegalAccessException, InstantiationException {
List<Field> fields = new ArrayList<Field>();
Field[] retrievedFields = entity.getClass().getDeclaredFields();
for (Field field : retrievedFields) {
fields.add(field);
}
T newEntity = (T) entity.getClass().newInstance();
for (Field field : fields) {
field.setAccessible(true);
field.set(newEntity, field.get(entity));
}
return newEntity;
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何获取字段的值。
我有3节课.
public interface Operation {
void move();
void delete();
void search(String criteria);
}
public abstract class AbstractOperationProcessor implements Operation {
public void move() {
// some logic
}
}
public class DailyMailProcessor extends AbstractOperationProcessor{
// need to hide this method because I don't want to provide them to customer
public void delete() {}
public void search(String criteria) {}
}
Run Code Online (Sandbox Code Playgroud)
我需要的是隐藏方法delete()和search(String)API.如何在不改变界面Operation和抽象类的情况下完成它AbstractOperationProcessor?
我在Scalaz使用bing操作时写了一个Fibonacci函数.这是我的代码:
import scalaz._, Scalaz._
def fib(i: Int): Option[Int] = i match {
case 0 => 0.some
case 1 => 1.some
case x if x > 1 =>
fib(i - 1) >>= {
a => fib(i - 2) >>= {
b => (a + b).some
}
}
case _ => None
}
Run Code Online (Sandbox Code Playgroud)
是否有可能简化它?我不喜欢这样的东西a => fib(i - 2) >>= ....
java ×5
javascript ×2
scala ×2
spring ×2
apache-spark ×1
dom ×1
github ×1
oop ×1
reflection ×1
scalaz ×1