volatile修饰符指示JVM访问volatile变量的线程应始终从内存中获取数据。即,线程不应缓存volatile变量。访问volatile变量会同步主内存中所有缓存的变量副本。volatile只能应用于对象类型或私有类型的实例变量。volatile对象引用可以为null。示例public class MyRunnable implements Runnable { private volatile boolean active; public void run() { active = true; while (active) { // line 1 ... 阅读更多
StringReader类是Reader类的子类,它可以用来读取字符串形式的字符流,该字符流充当StringReader的源。StringReader类重写了Reader类中的所有方法。StringReader类重要的有skip()、close()、mark()、markSupported()、reset()等方法。语法 Public class StringReader extends Reader 示例 import java.io.StringReader; import java.io.IOException; public class StringReaderTest { public static void main(String[] args) { String str = "Welcome to Tutorials Point"; StringReader strReader = new StringReader(str); ... 阅读更多
要查找和为给定数字的连续子数组,请执行以下操作:遍历数组。在每个元素中,逐个添加下一个n个元素,当和等于所需值时,打印子数组。示例import java.util.Arrays; import java.util.Scanner; public class sub_arrays { public static void main(String args[]){ //从用户处读取数组 Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that is to be created: "); int size = sc.nextInt(); int[] myArray = new int[size]; ... 阅读更多
要将整数数组中的零与非零元素分开并将它们推到末尾,您需要重新排列数组,将所有非零元素按顺序分配到其位置,从零开始。然后,从数组的最后位置到末尾填充零。示例以下Java程序将数组中的所有零推到末尾。import java.util.Arrays; import java.util.Scanner; public class ZerosFromNonZeros { public static void main(String args[]){ //从用户处读取数组 Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that ... 阅读更多
要查找Java数组中所有和等于给定数字的元素对,请执行以下操作:将数组中的每个元素添加到所有剩余元素(自身除外)。验证和是否等于所需数字。如果为真,则打印它们的索引。示例import java.util.Arrays; import java.util.Scanner; public class sample { public static void main(String args[]){ //从用户处读取数组 Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that is to be created: "); int size = sc.nextInt(); int[] myArray ... 阅读更多