Tag: 范围

Java检查区间中的数字是否为

可能重复: Java是否存在开放式间隔实现? 我有一个int变量,我想检查它的值是否在区间[a,b]中。 我知道使用x> = a和x <= b或实现一个可以做到这一点的简单方法是一个简单的问题,但我想知道是否已经做了一些事情。 搜索数学课,但我找不到。 这不是那么重要而不是那么大的问题,但我很好奇是否有这样的东西,所以我可以使用它而不是实现我自己的:) 在我的所有编码中,我都没有遇到过这样的方法。 也许你们其中一个java大师有:) 谢谢。

在一个扩展Java中另一个类的类中有两个具有相同名称的变量

以下是我的项目代码的一部分: public class Body extends Point{ public double x, y, mass; public Body() { x = y = mass = 0; } public Body(double x, double y, double mass) { this.mass = mass; this.x = x; this.y = y; } } public class Point { public double x; public double y; public Point(double x, double […]

scala范围与列表在大型集合上的性能

我为10,000,000个元素运行了一组性能基准测试,并且我发现每个实现的结果差别很大。 任何人都可以解释为什么创建Range.ByOne会导致性能优于简单的基元数组,但将相同范围转换为列表会导致性能甚至比最差情况更糟糕吗? 创建10,000,000个元素,并打印出模数为1,000,000的元素。 JVM大小始终设置为相同的最小值和最大值:-Xms?m -Xmx?m import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit._ object LightAndFastRange extends App { def chrono[A](f: => A, timeUnit: TimeUnit = MILLISECONDS): (A,Long) = { val start = System.nanoTime() val result: A = f val end = System.nanoTime() (result, timeUnit.convert(end-start, NANOSECONDS)) } def millions(): List[Int] = (0 to 10000000).filter(_ % 1000000 == 0).toList val results […]

更改方法中的变量值,Java

我有一个关于在Java中更改方法中的变量值的问题。 这是我的代码: public class Test { public static void funk(int a, int[] b) { b[0] = b[0] * 2; a = b[0] + 5; } public static void main(String[] args) { int bird = 10; int[] tiger = {7}; Test.funk(bird, tiger); } } 在执行方法Test.funk(bird, tiger) ,bird的值不会改变 – 它保留值10 ,即使在funk()方法中我们用a = b[0] + 5;更改了值a = b[0] […]

Spring:Singleton / session范围和并发

Spring bean的singleton / session范围是否要求必须同步对其所有字段的访问? 通过“synchronized”关键字或使用包“java.util.concurrent”中的某些类来说。 例如,这段代码不是线程安全的吗? (从这里复制/补充): @Component @SessionScoped public class ShoppingCart { private List items = new ArrayList(); public List getAllItems() { return items; } public void addItem(Product item) { items.add(item); } }

初学者Java:可变范围问题

我正在从我的java书中练习一些工作,我遇到了一个使用变量进行计算的方法的问题。 请注意,这是一项正在进行中的工作,我只是想让它使用circleArea方法来计算此刻的圆形区域。 这是必要的代码: public class Geometry { public static void printMenu() { System.out.println(“This is a geometry calculator\nChoose what you would like to calculate” + “\n1. Find the area of a circle\n2. Find the area of a rectangle\n3.” + ” Find the area of a triangle\n4. Find the circumference of a circle.” + “\n5. Find the […]

字符串索引超出范围:n

我每次执行它时都会遇到这个代码有点问题它给我一个错误字符串索引超出范围:’n’n – 是否。 在文本框中输入的与此代码相关的字符…(即文本框 – t2。)它停留在第一个文本框中,检查它不会转到下一个,如数组中所述。 Object c1[] = { t2.getText(), t3.getText(), t4.getText() }; String b; String f; int counter = 0; int d; for(int i =0;i<=2;i++) { b = c1[i].toString(); for(int j=0;j<=b.length();j++) { d = (int)b.charAt(j); if((d90)||(d122)) { counter++; } } } 它基本上是一个validation代码,我试图做没有例外和东西(仍在学习的过程:)) 非常感谢任何帮助。

如何指定随机数的范围?

我有二进制搜索树代码,随机插入数字。 我可以每次修改大小,但我想修改数字范围,例如:我希望随机数只是一位数或只是2位数。 我怎样才能做到这一点? public static void main( String[ ] args ) { BinarySearchTree bst = new BinarySearchTree( ); Random random = new Random( System.currentTimeMillis() ); int[] randoms = new int[1000]; Random randGen = new Random(); for(int i = 0; i < randoms.length; i++) { bst.insert( random.nextInt( randoms.length ) ); } System.out.println( "\n sorted :" ); […]

在Java中生成任意两个随机字符

如何在两个特定字符之间生成随机字符? 例如; 我想生成’h’或’v’中的任何一个。 谢谢

从单独的类文件访问公共静态类的状态

我在另一个公共类中有一个公共静态类,如下所示: public class Foo { public static class Bar{ A firstBar; Bar(A setBar){ this.firstBar=setBar; } } public final Bar instanceBar; public Foo(A actualValue) { instanceBar = new Bar(actualValue); } public Bar getBar() { return instanceBar; } 我的目标是从没有get方法的单独的类文件中访问instanceBar的状态,而不改变firstBar的可见性。 我该如何做到这一点? 例如,以下说不not visible 。 public class RetrieveFirstBar { public static void main(String[] args) { Foo z = new […]