Tag: 数组

如何在java中创建对象的子数组?

我有一个包含100个对象的对象数组。 我用10个对象创建了上面提到的数组的一个子。 现在我想剩下的90个对象作为一个新数组。 这是什么java代码?

如何在arrays中找到模式?

基本它的这个程序从用户输入6个数字,将它们存储在一个数组中,计算它们的均值和模式。 还要计算有多少数字大于均值。 我的程序代码是我错的地方 我的意思是正确的,但我的模式有问题。 包p18; import java.util.Arrays; import java.util.Scanner; public class P18 { public static void main(String[] args) { Scanner S=new Scanner(System.in); int[] arr1=new int [6]; for (int i = 0; i < 6; ++i) { int g = S.nextInt(); arr1[i] = g; } int input=6; double total=0d; double mean; for(int i=0;i<input;i++) { total=total+arr1[i]; } […]

我应该更改什么来使这个文件编译?

我在编程课程中,这个课程是我作业的一部分。 此文件分析名为“Names.txt”的文件中的数据,然后打印信息。 我收到编译错误,我想知道我需要更改或添加以使其成功编译。 这是我的代码: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class NameApp { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String selection, nameIn, nameIn2; Name name, name2; int decade; String first = “1”, second = “2”, third = “3”, fourth = “4”, fifth = “5”, sixth = “6”, seventh = […]

数组对象和普通对象初始化

我有3个对象root1,root2,root3 ,它们出现在root[] (Array Object)中,它是一个对象的集合。 我已经使用root[0]初始化了Node类。 所以我能够通过root[0].data访问Node类的值,但我希望root1.data也应该访问同一个类,因为root[0]=root1 ,但是我无法访问该值。 为了实现我的逻辑,我还需要做些什么吗? 我有以下程序: class a { static Node root1,root2,root3; public static void main(String args[]) { Node root[]={root1,root2,root3}; for(int i=0;i<root.length;i++) root[i]=new Node(value,null); System.out.println(root[i].data);//It is printing Correctly System.out.println(root1.data);//It is printing null value } public static class Node { int value; Node next; Node(int value,Node next) { this.value=value; this.next=next; } } }

Java程序使用数组简单编码项目

我只想要“Jan支出”出现,但只有支出出来。 我该怎么做? Jan来自我的monthsArrays。 有什么遗漏? import java.util.Scanner; public class Project { static int choice; public static void main(String[] args) { { Scanner input = new Scanner(System.in); System.out.println(“***************Expenditure***************”); System.out.println(“1)Enter monthly expenses”); System.out.println(“2)Display detailed expenditure by month”); System.out.println(“3)Quick glance at monthly expenses”); System.out.println(“4)Exit”); System.out.println(“Please select your choice :”); choice = input.nextInt(); switch (choice) { case 1: int count […]

Java对象数组java.lang.NullPointerException

我有一个问题,我创建一个对象数组但我得到一个java.lang.NullPointerException当我尝试解决它的问题。 这是有问题的class级。 public class Blocks { public static Block[] b = new Block[8]; public Blocks() throws IOException { new Air (b[0]); new Stone(b[1]); new Grass(b[2]); new Dirt (b[3]); } 这是类Block。 public class Block { private Texture Texture = null; private int S = World.BLOCK_SIZE; private boolean hasTexture = true; private String texturePath = null; public […]

数组Java中每个元素的长度

这是我正在尝试使用的。 方法.length对我尝试的任何东西都不起作用,所以我甚至不确定从哪里开始。 import java.util.ArrayList; public class LengthsOfStrings { public static ArrayList lengths(ArrayList list) { ArrayList lengthList = new ArrayList(); for (int nums: lengthList) { System.out.println(nums.length()); } return lengthList; } public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(“Ciao”); list.add(“Moi”); list.add(“Benvenuto!”); list.add(“badger badger badger badger”); ArrayList lengths = lengths(list); System.out.println(“The lengths of the […]

如何反向打印数组?

这是我的数组代码,我需要反向打印。 public class Array1D { public static void main(String[] args) { int[] array = new int[3]; array[0] = 1; array[1] = 2; array[2] = 5; for (int i = 0; i < array.length; i++) { //loop System.out.print("array["+i+"]="); System.out.println(array[i] + " "); } System.out.println("the last element in the matrix = " + array[array.length – 1]); // […]

如何排序2D数组?

我需要帮助排序2D数组。 我有两行数组 [5, 3, 4, 1, 2] [10,20,30,40,50] 我需要将它排序为这样: [1, 2, 3, 4, 5] [40,50,20,30,10] 我知道如何使用冒泡排序,但我需要一些更快的算法,例如quicksort。 这是我的冒泡排序代码 for (int i = 0; i < length-1; i++) { for (int j = 0; j array[0][j+1]) { for (int k = 0; k < 2; k++) { int tmp = array[k][j]; array[k][j] = array[k][j+1]; array[k][j+1]=tmp; } } […]

在具有两个缺失值的整数数组中查找2个缺少的数字

你怎么做到这一点? 值未排序但是[1..n]示例数组[3,1,2,5,7,8] 。 答案: 4, 6 我在另一篇类似的post中看到了这个解决方案,但我不明白最后一步 Find the sum of the numbers S=a1+…+an. Also find the sum of squares T=a1*a1+…+an*an. You know that the sum should be S’=1+…+n=n(n+1)/2 You know that the sum of squares should be T’=1^2+…+n^2=n(n+1)(2n+1)/6. Now set up the following system of equations x+y=S’-S, x^2+y^2=T’-T. Solve by writing x^2+y^2=(x+y)^2-2xy => xy=((S’-S)^2-(T’-T))/2. […]