我倾向于" if-conditional syndrome ",这意味着我倾向于一直使用条件.我很少使用三元运算符.例如:
//I like to do this:
int a;
if (i == 0)
{
a = 10;
}
else
{
a = 5;
}
//When I could do this:
int a = (i == 0) ? 10:5;
Run Code Online (Sandbox Code Playgroud)
我使用哪个问题?哪个更快?是否存在显着的性能差异?尽可能使用最短的代码是更好的做法吗?
如何检查列表是否为空?如果是这样,系统必须给出一条消息,说List是空的.如果没有,系统必须给出一条消息,说明列表不为空.用户可以输入数字,-1以停止该程序.这是我现在的代码,但这不起作用,它总是说'List is not empty'.
import java.util.*;
import javax.swing.JOptionPane;
public class ArrayListEmpty
{
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<Integer>();
int number;
do {
number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number (-1 to stop)"));
numbers.add(number);
} while (number != -1);
giveList(numbers);
}
public static void giveList(List<Integer> numbers)
{
if (numbers != null)
JOptionPane.showMessageDialog(null, "List isn't empty");
else
JOptionPane.showMessageDialog(null, "List is empty!");
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法使用DecimalFormat(或其他一些标准格式化程序)来格式化这样的数字:
1,000,000 => 1.00M
1,234,567 => 1.23M
1,234,567,890 => 1234.57M
基本上将一些数字除以1百万,保留2位小数,并在末尾打一个"M".我已经考虑过创建一个新的NumberFormat子类,但它看起来比我想象的要复杂.
我正在编写一个API,其格式方法如下所示:
public String format(double value, Unit unit); // Unit is an enum
Run Code Online (Sandbox Code Playgroud)
在内部,我将Unit对象映射到NumberFormatters.实现是这样的:
public String format(double value, Unit unit)
{
NumberFormatter formatter = formatters.get(unit);
return formatter.format(value);
}
Run Code Online (Sandbox Code Playgroud)
请注意,因为这个,我不能指望客户端除以100万,我不能只使用String.format()而不将它包装在NumberFormatter中.
Excel Scientific Number格式如下:
1,000,000 >> 1.00E+06
330,000 >> 3.30E+05
Run Code Online (Sandbox Code Playgroud)
我怎样才能转换为这种格式:
1,000,000 >> 1M
330,000 >> 330K
Run Code Online (Sandbox Code Playgroud)
(使用千克,兆,毫等)
可能重复:
格式化文件大小为MB,GB等
使用NumberFormat,我想将我的数字格式化为科学乘数.换句话说,我想要以下格式:
显然,其他数字应使用k,G和其他倍数表示.
我怎样才能做到这一点 ?或者我需要一些Java库?
我想将以下数字格式化为Android旁边的数字:
1000至1k 5821至5.8k 2000000至2m 7800000至7.8m
java ×5
formatting ×2
android ×1
arraylist ×1
excel ×1
format ×1
notation ×1
numbers ×1
numeric ×1
optimization ×1
performance ×1
string ×1