问题描述
美好的一天,我的 Java 代码中有这个错误。我正在尝试将我的数据库链接到 azure cloud。我使用了列表命令:
Exception in thread "main" java.lang.indexoutofboundsexception: Index: 11,Size: 11
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at anothercloud.Anothercloud.main(Anothercloud.java:63)
Java Result: 1
package anothercloud;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
*
* @author ariel
*/
public class Anothercloud {
static HashMap<Double,String> redisData = new HashMap<Double,String>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean useSsl=true;
inclass infos=new inclass();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> value = new ArrayList<String>();
String anjiecachekey="xxxx=";
JedisShardInfo shardInfo=new JedisShardInfo("anjiecloud.redis.cache.windows.net",6380,useSsl);//use secondary connection string
shardInfo.setPassword("xxxx=");//use secondary access key
//Simple PING command
Jedis jedis=new Jedis(shardInfo.getHost());
try{
jedis.auth(anjiecachekey);
jedis.connect();
System.out.println("My Cache Response: "+jedis.ping());
if (jedis.llen("state") == 0 && jedis.llen("number") == 0) {
for(Map.Entry m: inclass.map.entrySet()){
jedis.lpush("state",(String)m.getValue());
jedis.lpush("number",m.getKey().toString());
}
}
for(String s: jedis.lrange("state",1000)){
names.add(s);
}
for(String r: jedis.lrange("number",1000)){
value.add(r);
}
for(int i =0; i < names.size(); i++){
redisData.put(Double.parseDouble(value.get(i)),names.get(i));
}
ArrayList<String> states = new ArrayList<String>();
for (Map.Entry m : redisData.entrySet()) {
states.add((String)m.getValue());
}
String[] statesArray = new String[states.size()];
states.toArray(statesArray);
JComboBox<String> stateList = new JComboBox<>(statesArray);
stateList.addItemListener(new MyHandler());
JFrame jframe = new JFrame();
JLabel item1 = new JLabel("IGR Statistics H1 2020");
jframe.add(item1);
jframe.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new FlowLayout());
jframe.setSize(400,200);
jframe.setVisible(true);
jframe.add(stateList);
// get the selected item:
// String selectedBook = (String) stateList.getSelectedItem();
// check whether the server is running or not
System.out.println("Server is running: " + jedis.ping());
//getting the percentage for each state
// storing the data into redis database
for (Map.Entry m : inclass.map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
}
catch(JedisConnectionException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,"You require a working internet connection");
}
}
static class MyHandler implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
for (Map.Entry m : redisData.entrySet()) {
if(e.getItem().toString() == m.getValue()&& e.getStateChange() == 1){
JOptionPane.showMessageDialog(null,m.getKey(),"VALUE IN BILLIONS",1);
System.out.println(m.getKey());
break;
}
}
}
}
}
请问我该如何纠正这个错误?任何反馈将不胜感激。我是 Java 作为一种语言及其语法的新手。该错误似乎也来自我的 for 循环。错误似乎也来自我的 for 循环。
解决方法
正如错误所述,您看到的是 IndexOutOfBoundsException
。这意味着您正在尝试访问大于/小于其范围的列表索引。在这种情况下,value.get(i)
抛出错误:
for (int i = 0; i < names.size(); i++)
{
redisData.put(Double.parseDouble(value.get(i)),names.get(i));
}
尝试修改如下代码:
for (int i = 0; i < names.size() - 1; i++)
{
redisData.put(Double.parseDouble(value.get(i)),names.get(i));
}
此外,我建议您重新访问 Redis 缓存的 Java 实现:Intro to Jedis – the Java Redis Client Library