我试图通过以下方法分离5.6(例如):
private static double[] method(double d)
{
int integerPart = 0;
double fractionPart = 0.0;
integerPart = (int) d;
fractionPart = d - integerPart;
return new double[]{integerPart, fractionPart};
}
Run Code Online (Sandbox Code Playgroud)
但我得到的是:
[0] = 5.0
[1] = 0.5999999999999996
Run Code Online (Sandbox Code Playgroud)
如果不将数字转换为字符串,您对此有何建议?
我想用bash shell压缩文件,所以我使用了:
echo -n 'Insert the file path:'
read path
echo 'Hello World' > ${path}
zip -u ${path}.zip ${path}
Run Code Online (Sandbox Code Playgroud)
当我运行这个脚本时,它会给我一个警告:
zip warning: test.zip not found or empty
adding: test (deflated 66%)
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我如何禁用此警告?我zip
是以正确的方式使用的吗?
我有一个应用程序,使用蓝牙从其他设备接收一些数据(字节).一切进展顺利,但我在接收所有字节时遇到了一个小问题.收到字节后,我在Toast上显示它们只是为了测试它们.当另一个设备一起发送10个字节时(例如:"ABCDEFGHIJ"),程序将仅采用第一个字节"A"并在Toast上显示,然后转到第二个迭代并读取其他9个字节并显示" BCDEFGHIJ"在吐司上.这是我的代码:
byte[] buffer = new byte[1024]; // Read 1K character at a time.
int bytes = 0; // Number of bytes.
while(true)
{
try
{
// Read from the InputStream.
bytes = bInStream.read(buffer);
// Send the obtained bytes to the MainActivity.
mainActivityHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
catch(IOException e)
{
connectionLost();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
在MainActivity中,我有:
// The Handler that gets information back from the BluetoothManager.
private final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what) …
Run Code Online (Sandbox Code Playgroud) 我想将一个ArrayList保存到SharedPreferences,所以我需要把它变成一个字符串然后回来,这就是我正在做的事情:
// Save to shared preferences
SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("myAppsArr", myAppsArr.toString());
editor.commit();
Run Code Online (Sandbox Code Playgroud)
我可以检索它String arrayString = sharedPref.getString("yourKey", null);
但我不知道如何将arrayString转换回ArrayList.怎么做到呢?
我的数组看起来像:
[item1,item2,item3]
Run Code Online (Sandbox Code Playgroud) 如何在日历之间进行转换?这是我有的:
UmAlQuraCalendar hijri = new UmAlQuraCalendar();
GregorianCalendar cal = new GregorianCalendar();
DateTime hijriDate = new DateTime(1434, 11, 23, hijri);
DateTime gregorianDate = ...; //
Run Code Online (Sandbox Code Playgroud)
我需要一个gregorianDate
对应的hijriDate
.
实际上,这是我第一次看到这样的代码:
class A
{
public static void main(String args[])
{
outer : for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(j > i)
{
System.out.println();
continue outer;
}
System.out.print(" " +( i *j ));
}
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
两行我不明白:
outer : for(int i=0;i<10;i++) // this seems similar to 'for each'?
continue outer; // I know that 'continue' will break the loop and continue the next turn, but what will do in this situaton?
Run Code Online (Sandbox Code Playgroud) 我有一个有3列的JTable:
- No. #
- Name
- PhoneNumber
Run Code Online (Sandbox Code Playgroud)
我想为每列制作特定的宽度,如下所示:
我希望JTable能够在需要时动态更新其列的宽度(例如,在列中插入大数字#
)并保持JTable的相同样式
我使用以下代码解决了第一个问题:
myTable.getColumnModel().getColumn(columnNumber).setPreferredWidth(columnWidth);
Run Code Online (Sandbox Code Playgroud)
但是,如果列的当前宽度不适合其内容,我没有成功使myTable动态更新宽度.你能帮我解决这个问题吗?
我JScrollPane
跟JTextArea
里面,我试图从右侧设置的JTextArea的方向向左所以里面的文字就会从右边开始,滚动条会出现在左边
我尝试过以下但是它们并没有影响方向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)
编辑:
两个答案camickr和trashgod提供的工作正常但不在我的程序中我使用我的JTextArea作为对象消息并将其传递给OptionPane.
EDIT2:
我发现,setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
如果我将它应用于JOptionPane内容,则无效.是否有替代方案解决此问题?
与我的代码类似:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
private JTextArea txt = new JTextArea();
public TextArea()
{
setLayout(new GridLayout());
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(txt);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setPreferredSize(new Dimension(200,200));
this.add(scroll);
}
private void display()
{
Object[] options = {this};
JOptionPane pane = new JOptionPane();
int option = pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
}
public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud) 我知道用的是什么ArrayList<T>
,但我什么时候应该使用ArrayList<?>
?你能用例子解释一下吗?谢谢.