public class Main {
public static void main(String[] args) {
ArrayList<Integer> ar = new ArrayList<Integer>();
List l = new ArrayList();
l.add("a");
l.add("b");
ar.addAll(l);
System.out.println(ar);
}
}
Run Code Online (Sandbox Code Playgroud)
输出: [a,b]
你不能直接添加String
到ArrayList<Integer> ar
,但通过使用addAll()
是可能的.
我们如何可以添加String
到ArrayList
其类型已经被指定为Integer
?任何人都可以突出显示清晰的实施细节和背后的原因吗
public class Main{
public static void main(String[] args) throws Exception {
// Creating objects for class Check(2 different objects)
Check c = new Check("s1");
Check c1 = new Check("s2");
c.start();c1.start();
}
}
class Check extends Thread{
Check(String name){super(name);}
private Integer ab = 2;
public void run(){
synchronized (ab) {
System.out.println(Thread.currentThread().getName());
for(int i=0;i<10;i++)System.out.print(i+" ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里我已经对变量ab进行了同步.我已经创建了两个不同的类Check实例,但是我总是得到s1的输出,然后是s2,反之亦然,但是没有混合,为什么会这样呢?当我已经创建了两个单独的对象(在main中),那么两个不同的线程,两个不同的ab变量,那么它如何成为两个不同对象的共享资源?
我想Fragment
用新的替换旧的Fragment
,但我仍然得到旧的按钮,Fragment
在新片段中仍然可见.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment newFragment = GenericMood.newInstance("a","b");
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.allmoods, newFragment);
transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
Run Code Online (Sandbox Code Playgroud)
我可以用Fragment
新的替换旧的,但是R.id.allmoods的按钮Fragment
仍然可以在新的顶部看到Fragment
.
我尝试使用下面给出的代码.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment newFragment = GenericMood.newInstance("a","b");
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed …
Run Code Online (Sandbox Code Playgroud) 我已经在编码平台中看到了此代码,可以有效地计算出不同值的欧拉糖度。我无法理解此实现。我真的很想学这个。谁能帮我解释一下吗?
for(int i = 1; i < Maxn; i++) { // phi[1....n] in n * log(n)
phi[i] += i;
for(int j = 2 * i; j < Maxn; j += i) {
phi[j] -= phi[i];
}
}
Run Code Online (Sandbox Code Playgroud) 我编写了一个代码来更改按钮的minWidth,以便我可以摆脱按钮文本周围的填充.
for(int i=0;i<26;i++){
Button alphabet = new Button(getContext());
alphabet.setText(""+(char)('A'+i));
alphabet.setMinWidth(0);
allAlphabets.addView(alphabet);
}
Run Code Online (Sandbox Code Playgroud)
我在每个按钮中都有一个字母,最后动态地将它添加到布局中.由于填充,它占用了很多空间,因为我已经将最小宽度设置为0,但它仍然不起作用.它显示相同的默认按钮.请帮忙.