Tag: 数组

为什么不能在Java构造函数中使用字段的简写数组初始化?

采用以下示例: private int[] list; public Listing() { // Why can’t I do this? list = {4, 5, 6, 7, 8}; // I have to do this: int[] contents = {4, 5, 6, 7, 8}; list = contents; } 为什么我不能使用速记初始化? 我能想到解决这个问题的唯一方法是为该数组创建另一个数组和设置list 。

Java – 包含数组字段的枚举

我想将每个名称的列表名称和单个昵称存储为Java中的Enum。 昵称的数量不会有所不同。 目的是能够从昵称中获取全名。 目前我已经实现了这样: public enum Names { ELIZABETH(new String[] {“Liz”,”Bet”}), … ; private String[] nicknames; private Names(String[] nicknames) { this.nicknames = nicknames } public Names getNameFromNickname(String nickname) { //Obvious how this works } } 我不喜欢不得不重复new String[] {…} ,所以我想知道是否有人可以建议一种替代的,更简洁的实现方法? 干杯, 皮特

如何使用GSON将List转换为JSON对象?

我有一个List,我需要使用GSON转换为JSON对象。 我的JSON对象中包含JSON数组。 public class DataResponse { private List apps; // getters and setters public static class ClientResponse { private double mean; private double deviation; private int code; private String pack; private int version; // getters and setters } } 下面是我的代码,我需要将我的List转换为JSON对象,其中包含JSON数组 – public void marshal(Object response) { List clientResponse = ((DataResponse) response).getClientResponse(); // now how do […]

找到数组中相同数字之间的最大跨度

圣诞快乐,希望你精神振奋,我在Java-Arrays中有一个问题,如下所示。我坚持不懈地努力获得它。 Consider the leftmost and righmost appearances of some value in an array. We’ll say that the “span” is the number of elements between the two inclusive. A single value has a span of 1. Write a **Java Function** that returns the largest span found in the given array. **例: maxSpan({1,2,1,1,3})→4,答案是4 coz MaxSpan在1到1之间是4 maxSpan({1,4,2,1,4,1,4})→6,答案是6考斯MaxSpan 4到4之间是6 […]

java中的动态数组合并

我有两个像这样的数组。 String[] arr1 = { “1”, “2”, “3” }; String[] arr2 = { “111”, “222”, “333”, “444”, “555”, “666”, “777”, “888”, “999” }; 我想使用索引值的组合合并这两个数组。 我的输入将是两个整数值(2:3比例),就像这样 int firstArray = 2; //input value int secondArray = 4; //input value 合并后,所有值都将存储在单个列表中。 现在我需要像这样的输出。 1 2 111 222 333 444 3 1 555 666 777 888 2 3 999 111 […]

解析Android中的JSON数组和对象

这就是JSON的样子: [{ “pmid”: “2”, “name”: ” MANAGEMENT”, “result”: “1”, “properties”: [ { “prop_id”: “32”, “prop_name”: “Bonneville”, “address”: “122 Lakeshore”, “city”: “Ripley”, “state”: “OH”, “zip”: “11454”, “lat”: “41.123”, “long”: “-85.5034” } ] }] 我试图用Android中的以下Java代码解析它: JSONObject jObj = null; 试试{jObj = new JSONObject(jsonStr); // We get weather info (This is an array) JSONArray jArr = jObj.getJSONArray(“properties”); // […]

找到最长的连续数字序列

问题H(最长的自然inheritance者): 如果第二个整数是自然数序列中第一个的inheritance者,则两个连续的整数是自然后继的(1和2是自然后继者)。 编写一个读取数字N后跟N个整数的程序,然后打印连续自然后继的最长序列的长度。 例: 输入7 2 3 5 6 7 9 10输出3这是我的代码到目前为止,我不知道为什么它不起作用 import java.util.Scanner; public class Conse { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int[] array = new int[x]; for (int i = 0; i < array.length; i++) { array[i] = scan.nextInt(); } System.out.println(array(array)); } public […]

一系列字符串与字符串Varargs

void method(String[] a)和void method(String… a)什么区别? 第一种方法采用字符串数组,其中第二种方法采用一个或多个String参数。 它们提供了哪些不同的function? 此外,不知道为什么,但这是有效的: public class Test { public static void main(String[] args) { String[] a = {“Hello”, “World”}; Test t = new Test(); t.method(a); } void method(String…args) { System.out.println(“Varargs”); // prints Varargs } }

java:基于array2排序array1

感谢Zirak的帮助在我之前的post中,我在JavaScript中实现了以下内容: var arr1 =[0,1,2,3]; var arr2 =[“ac”, “bc”, “ad”, “e”]; var result = arr1 .sort(function(i, j){return arr2[i].localeCompare(arr2[j])}) document.write(result ); 实现这一点的方法在JavaScript中非常紧凑,这样的简单实现也可以实现这一点的java实现吗? 我只能想到实现Comparable接口,如下所示: public class testCompare { public static String[] arr2={“ac”, “bc”, “ad”, “e”}; public static Obj[] arr1={new Obj(0), new Obj(1), new Obj(2), new Obj(3)}; static class Obj implements Comparable{ int index=0; public Obj(int i){ index=i; } […]

在Java中将文本文件转换为二维不规则数组

嘿伙计们这是我上一个问题的后续内容。 我现在有一个文本文件格式如下: 100 200 123 124 123 145 我想要做的是将这些值放入Java中的二维不规则数组中。 到目前为止我所拥有的是: public String[][] readFile(String fileName) throws FileNotFoundException, IOException { String line = “”; ArrayList rows = new ArrayList(); FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); while((line = br.readLine()) != null) { String[] theline = line.split(“\\s”);//TODO: Here it adds the space between two numbers […]