我想编写Java代码来生成范围[1,4]中的随机整数数组.数组的长度为N,在运行时提供.问题是范围[1,4]不均匀分布:
这意味着如果我创建N = 100的数组,数字'1'将在数组中平均出现40次,数字'2'出现10次,依此类推.
现在我使用此代码生成范围[1,4]中的均匀分布的随机数:
public static void main(String[] args)
{
int N;
System.out.println();
System.out.print("Enter an integer number: ");
N = input.nextInt();
int[] a = new int[N];
Random generator = new Random();
for(int i = 0; i < a.length; i++)
{
a[i] = generator.nextInt(4)+1;
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用非均匀分布实现它,如上图所示?
java arrays random non-uniform-distribution probability-density
我需要确保一件商品只卖一次.这也必须通过多个线程来确保.
是否足以检查buyer变量是否为空?所以在我看来,第二个来电者会收到一个AlreadyBoughtException?
public synchronized void buy(Buyer buyer) throws AlreadyBoughtException {
if (this.buyer != null) {
throw new AlreadyBoughtException();
}
System.out.println(buyer + " bought article " + identifier);
this.buyer = buyer;
this.sold = true;
}
Run Code Online (Sandbox Code Playgroud)
这是否是线程安全的,我可以假设当buy同时调用该方法(完全相同的时间)时,不可能无法购买该文章吗?
我想将在main方法中创建的确切实例传递给具有MPGui作为参数的新Executor.这可能吗?
public class MPGui {
public MPGui() {
//initialize GUI
}
public class ExecuteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Executor execu = new Executor(MLA, /*the MPGUI() instance */);
execu.execute();
}
}
public static void main(String[] args) {
MPGui a = new MPGui();
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个关于Android和PHP/MySQL之间关系的问题.
如果我使用版本3及更高版本是真的,我需要在后台使用单独的线程进行连接?
是否有必要使用JSON来获取答案?
我在不使用多线程和JSON的情况下编写代码,但它仅适用于2.3及更高版本.我尝试了4.0和4.2,但它没有回复任何回复.
我认为对象只存在于已定义的范围内.但是在这个程序中,执行thtest方法后,t1线程的对象仍然存在,我可以在输出中看到"hi"和"bye".
public class apples {
public static void main(String args[]){
int b =1;
navid n = new navid();
n.thtest();
while (b==1){ System.out.println("bye"); }
}
}
Run Code Online (Sandbox Code Playgroud)
public class navid {
int a=1;
public void thtest (){
Runnable te = new Runnable() {
public void run(){
try{
while (a==1){ System.out.println("hi");}
}catch(Exception e){}
}
};
Thread t1 = new Thread (te);
t1.start();
}
}
Run Code Online (Sandbox Code Playgroud) 这里有一个while循环我有这个问题:
while((input = InputHandler.getInt()) != 1 && input != 2){
if(input == InputHandler.CODE_ERROR)
System.out.print("Input must be a number");
}
Run Code Online (Sandbox Code Playgroud)
这个while循环只接受一次输入而不再请求它,所以它循环使用整个时间输入的输入.我在这里做错了什么,因为对我来说这个wile循环工作真的很奇怪?
InputHandler类:
public class InputHandler {
public static Scanner in = new Scanner(System.in);
public static int CODE_ERROR = -6;
public static int getInt(){
try{
return in.nextInt();
} catch(InputMismatchException e){
return CODE_ERROR;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我对这段代码有些问题.我正在尝试使用以下格式创建动态新闻列表:
|image| - title
|image|- subtitle
Run Code Online (Sandbox Code Playgroud)
这是我的小cicle代码(随机数据的一个例子)
void setListNews(List<Map<String,String>>l){
listaNewsPagina = l;
final Iterator ite = listaNewsPagina.iterator();
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
LinearLayout lineare = (LinearLayout)findViewById(R.id.lineare);
while(ite.hasNext()){
Map<String,String> map = (Map<String, String>) ite.next();
ImageView imm = (ImageView)findViewById(R.id.immagine);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relative);
TextView titolo = (TextView)findViewById(R.id.titolo);
TextView sottoTitolo = (TextView)findViewById(R.id.sottoTitolo);
titolo.setText("titolooooo");
sottoTitolo.setText("sottoTitoloooooooooooo");
rl.addView(titolo);
rl.addView(sottoTitolo);
lineare.addView(imm);
lineare.addView(rl);
}
lin.addView(lineare);
setContentView(lin);
Run Code Online (Sandbox Code Playgroud)
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_sezione_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_homepage" >
<LinearLayout
android:id="@+id/lineare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aadd99"
>
<ImageView
android:id="@+id/immagine"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp" …Run Code Online (Sandbox Code Playgroud) 如何在字符串中找到字符的第二个索引.例如:
String a="aa{aaaaaaa{aaa}";
Run Code Online (Sandbox Code Playgroud)
我想找到第二个的索引值{.这是10.
任何人都可以解释以下结果吗?
Path p = Paths.get("/a/b");
System.out.println(p.startsWith(p.subpath(0, 1)));
Run Code Online (Sandbox Code Playgroud)
输出:
false
我希望它是真的,因为路径从路径的第一个元素开始.
这是我的网址
String serverUrl = http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/Universal Image Loader @#&=+-_.,!()~'%20.png
Run Code Online (Sandbox Code Playgroud)
这Universal Image Loader @#&=+-_.,!()~'%20.png只是动态赋予字符串的文件名。
HttpPost httpPost = new HttpPost(serverUrl);
Run Code Online (Sandbox Code Playgroud)
我得到:
java.lang.IllegalArgumentException: Illegal character in path at index 76: http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/Universal Image Loader @#&=+-_.,!()~'%20.png
有什么补救办法?