gpt4 book ai didi

Android ArrayList 的 int

转载 作者:行者123 更新时间:2023-11-30 01:00:28 25 4
gpt4 key购买 nike

我是 android 和 java 的新手,所以如果我说的不完全正确,我深表歉意。在我的应用程序中,我想将 ArrayList 转换为整数将每个值增加 +1。请注意注释掉的部分以了解我的问题在哪里。我找不到正确的方法..这就是我现在所做的:

public String mRequest(String mUrl, String mAuth, String mParam, String customerId, ArrayList filter) {

InputStream mStreamResponse;
String mString = null;

try {

URL obj = new URL(mUrl);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", mAuth);
con.setRequestProperty("X-Limit", String.valueOf(xLimit));
con.setRequestProperty("X-Skip", String.valueOf(xSkip));
con.setRequestProperty("X-Sort", "{\"created\":-1}");

String parameters = null;

System.out.println("Value of mParam -> " + mParam);

if (mParam != null && customerId != null) {
Log.e("ERROR", "Ricerca per parametro e customerId");
} else if (mParam != null) {
parameters = "\"number\":{\"$regex\":" + mParam + "}";
} else if (customerId != null) {
parameters = "\"customer.id\":{\"$eq\":" + "\"" + customerId + "\"" + "}";
} else {
parameters = "\"number\":{\"$regex\":\"\"}";
}

System.out.println("parameters");
System.out.println(parameters);

if (filter != null) {

//convert ArrayList to Array
Object[] mArray = filter.toArray();

String filterGroup = mArray[0].toString() + ".id";
System.out.println("Group selected -> " + filterGroup);

for (int i = 1; i < mArray.length; i++) {
System.out.println("Value -> " + mArray[i]);
}

String pFilter = null;

for (int i = 1; i < mArray.length; i++) {

/*
*
* This is where I need to change value in
* pos i with i + 1
*
*/


switch (mArray[i].toString()) {
case "0":
mArray[i] = "1";
break;
case "1":
mArray[i] = "2";
break;
case "2":
mArray[i] = "3";
break;
case "3":
mArray[i] = "4";
break;
case "4":
mArray[i] = "5";
break;
case "5":
mArray[i] = "6";
break;
case "6":
mArray[i] = "7";
break;
case "7":
mArray[i] = "8";
break;
case "8":
mArray[i] = "9";
break;
case "9":
mArray[i] = "10";
break;
case "10":
mArray[i] = "11";
break;
case "11":
mArray[i] = "12";
break;
case "12":
mArray[i] = "13";
break;
}

if (mArray.length == 2){
pFilter = mArray[i].toString();
} else {

if (mArray[i] != mArray[mArray.length - 1]) {

if (pFilter != null) {
pFilter = pFilter + mArray[i].toString() + ",";
} else {
pFilter = mArray[i].toString() + ",";
}

} else {
pFilter = pFilter + mArray[i].toString();
}
}

}

String mFilter = "[" + pFilter + "]";
System.out.println("Insert value in a string -> " + mFilter);

String tempParam = null;
if (filterGroup.equals("assignee")) {
tempParam = "{\"$eq\":" + mFilter + "}";
} else {
tempParam = "{\"$in\":" + mFilter + "}";
}

//Override the value with the same filterGroup
if (filterMap.containsKey(filterGroup)) {
String toOverride = filterGroup;
filterMap.remove(filterGroup);
filterMap.put(toOverride, tempParam);
} else {
filterMap.put(filterGroup, tempParam);
}

//iterate
for (Map.Entry<String, String> entry : filterMap.entrySet()) {

switch (entry.getKey()) {
case "status.id":
status = "\"" + entry.getKey() + "\":" + entry.getValue();
break;
case "queue.id":
queues = "\"" + entry.getKey() + "\":" + entry.getValue();
break;
case "type.id":
types = "\"" + entry.getKey() + "\":" + entry.getValue();
break;
case "severity.id":
severities = "\"" + entry.getKey() + "\":" + entry.getValue();
break;

case "assignee.id":

mytickets = "\"" + entry.getKey() + "\":" + entry.getValue();
break;

}

}

filterMapValues = parameters ;

if (status != null) {
filterMapValues += ", " + status;
}

if (queues != null) {
filterMapValues += ", " + queues;
}

if (types != null) {
filterMapValues += ", " + types;
}

if (severities != null) {
filterMapValues += ", " + severities;
}

if (mytickets != null) {
filterMapValues += ", " + mytickets;
}

String myFilter = "{" + filterMapValues + "}";
System.out.println("myFilter -> " + myFilter);

//setup request header
con.setRequestProperty("X-Filter", myFilter);

} else {

String xFilter = "{\"status.id\":" + statusAll + ", \"queue.id\":" + queueAll + ", \"type.id\":" + typeAll + ", \"severity.id\":" + severityAll + "," + parameters + "}";
con.setRequestProperty("X-Filter", xFilter);
System.out.println("Reset all -> " + xFilter);

}

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + mUrl);
System.out.println("Response Code : " + responseCode);

mStreamResponse = con.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(mStreamResponse));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
mStreamResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}

mString = sb.toString();

} catch (Exception e) {

}


return mString;
}

我该怎么办?

提前致谢。

编辑我在 pos #1 中启动了 for 循环,因为在 pos #0 中我保存了 filterGroup 值,这是一个字符串,我需要管理 pos > 0 中的唯一值

最佳答案

首先:

I'd like to convert an ArrayList to integer [...]

ArrayList<E>是一个 collection 对象,类型为 E,可以是任何对象或基元。您真正要求的是如何转换 ArrayList<String> (看到它是一个 ArrayList 类型为 String)到一个整数数组。我可以看到你已经转换了 ArrayList<String>进入 Object[] (可以保存整数和字符串的对象数组)在上面给出的示例代码中使用以下行:

Object[] mArray = filter.toArray();

回到原来的答案,这样就可以了:

    int[] mArray = new int[filter.size()];
for (int i = 0; i < filter.size(); i++) {
int value = Integer.parseInt(filter.get(i));
mArray[i] = value++;
}

关于Android ArrayList 的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com