这是我的2d阵列和单个阵列的结构
public String[] sList() {
String list[] = { "Asia Medic Family Hospital and Medical Center",
"Adventist University of the Philippine Health Service ",
"Bautista Hospital",
"Catalan Medical Hospital ",
"Catbagan General Hospital",
"Cavite Center for Mental Health",
"Cavite Doctors' Medical Center, Inc",
"Cavite Medical Center",
"Cavite Municipal Hospital",
"Cavite Naval Hospital",
"Contreras Medical Clinic",
"Crisostomo General Hospital",
"DBB Municipal Hospital",
"DLSU-Dr. Rodolfo Poblete Memorial Hospital ",
"De La Salle University Medical Center",
"Del Rosario Hospital" };
return list;
.....
public String[][] sSpecialty() {
String list[][] = {
{"Specialty 1.0", "Specialty1.1", "Specialty1.2" , " " , " " },
{"Specialty 2.0", "Specialty2.1", "Specialty2.2" , " " , " "},
{"Specialty 3.0", "Specialty3.1", "Specialty3.2", "Specialty3.3", " " },
{"Specialty 4.0", "Specialty4.1", "Specialty4.2", "Specialty4.3", "Specialty4.4" },
{"Specialty 5.0", "Specialty5.1", "Specialty5.2", "Specialty5.3", "Specialty5.4" },
{"Specialty 6.0", "Specialty6.1", "Specialty6.2", "Specialty6.3", "Specialty6.4" },
{"Specialty 7.0", "Specialty7.1", "Specialty7.2", "Specialty7.3", "Specialty7.4" },
{"Specialty 8.0", "Specialty8.1", "Specialty8.2", "Specialty8.3", "Specialty8.4" },
{"Specialty 9.0", "Specialty9.1", "Specialty9.2", "Specialty9.3", "Specialty9.4" },
{"Specialty 10.0", "Specialty10.1", "Specialy10.2", "Specialty10.3", "Specialty10.4" },
{"Specialty 11.0", "Specialty11.1", "Specialty11.2", "Specialty11.3", "Specialty11.4" },
{"Specialty 12.0", "Specialty12.1", "Specialty12.2", "Specialty12.3", "Specialty12.4" },
{"Specialty 13.0", "Specialty13.1", "Specialty13.2", "Specialty13.3", "Specialty13.4" },
{"Specialty 14.0", "Specialty14.1", "Specialty14.2", "Specialty14.3", "Specialty14.4" },
{"Specialty 15.0", "Specialty15.1", "Specialty15.2", "Specialty15.3", "Specialty15.4" },
{"Specialty 16.0", "Specialty16.1", "Specialty16.2", "Specialty16.3", "Specialty16.4" }
};
return list;
我如何过滤这些二维数组,例如,当我选择Specialy2.2时,我在这里有一个项目,结果是菲律宾基督复临安息日会大学卫生服务中心,你可以看到它们都在第二个数组列表中。我很好地过滤了单个数组,但在添加2d数组时遇到了问题。
for (int loop = 0; loop < sList.length; loop++) {
String temp = sTuition[loop];
theTuitionFee = Integer.parseInt(temp);
// ALL filter selected
if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee
&& sSpecialty[loop][loop].equalsIgnoreCase(thisFilter[5])
) {
list.add(sList[loop]);
我在"&&sSpecialty[loop][loop].equalsIgnoreCase(thisFilter[5]("这一部分中得到了ArrayIndexOutofBoundsException关于如何解决这个问题的任何建议。
如果你有其他方法添加创建的2d数组,这是我只使用单个数组时的完整程序。
public class SchoolListFragmentFilter extends Fragment implements
OnItemClickListener {
NavigationDrawer navi = new NavigationDrawer();
String thisFilter[] = NavigationDrawer.theFilter;
int amountTF = NavigationDrawer.TuitionFee;
PSManilaDB conn = new PSManilaDB();
String sList[] = conn.sList();
String sReg[] = conn.sRegAffil();
String sAdmin[] = conn.sAdmin();
String sType[] = conn.sSize();
String sAmbience[] = conn.sAmbience();
String sTuition[] = conn.sTuitionFee();
int theTuitionFee;
ListView lvList;
ArrayAdapter<String> mAdapter;
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> schoolIndex = new ArrayList<Integer>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater
.inflate(R.layout.layout_school_list, container, false);
lvList = (ListView) v.findViewById(R.id.lvSchools);
for (int loop = 0; loop < sList.length; loop++) {
String temp = sTuition[loop];
theTuitionFee = Integer.parseInt(temp);
// ALL filter selected
if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
// RegAffil
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
// Administration
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
// RoomAmbience
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
// Tuition Fee
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
// regAffil and admin
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
// regAffil and type
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
// regaffil and ambience
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
// reg afill and tuition
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
// admin and type
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
// admin and ambience
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
// admin and tuition
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
// type and ambience
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
// type and tuition
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
// ambience and tuition
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*") && amountTF == 0) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (thisFilter[0].equalsIgnoreCase("*")
&& thisFilter[1].equalsIgnoreCase("*")
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (thisFilter[0].equalsIgnoreCase("*")
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF == 0) {
list.add(sList[loop]);
} else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& thisFilter[3].equalsIgnoreCase("*")
&& amountTF >= theTuitionFee) {
list.add(sList[loop]);
}
}
mAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);
lvList.setAdapter(mAdapter);
lvList.setOnItemClickListener(this);
schoolIndex.clear();
ArrayList<String> finalFilterList = list;
ArrayList<String> finalSchoolsList = new ArrayList<String>(
Arrays.asList(this.sList));
for (int i = 0; i < finalFilterList.size(); i++) {
for (int o = 0; o < finalSchoolsList.size(); o++) {
if (finalFilterList.get(i).equalsIgnoreCase(
finalSchoolsList.get(o))) {
schoolIndex.add(o);
}
}
}
mAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);
lvList.setAdapter(mAdapter);
lvList.setOnItemClickListener(this);
if (list.isEmpty()) {
Toast.makeText(getActivity(), "No Schools Found",
Toast.LENGTH_SHORT).show();
navi.dia.show();
}
return v;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
int indx = schoolIndex.get(position);
Toast.makeText(getActivity(), list.get(position), Toast.LENGTH_SHORT)
.show();
Intent woahIntent = new Intent("capstone.app.recommender.SCHOOLINFO");
woahIntent.putExtra("key", indx);
startActivity(woahIntent);
}
// && sAdmin[count].equals(thisFilter[1])
// && sType[count].equals(thisFilter[2]))
}
如果我理解正确的话,sList.length
是16,但您的2d数组宽度只有5。这意味着loop
上升到16,但sSpecialty[loop][loop]
中的第二个索引必须小于5。这就是获取IndexOutOfBoundsException的地方。
我想你需要用之类的东西来代替if语句
// ALL filter selected
boolean filtermatch=false;
for(int subloop=0;subloop<sSpecialty[loop].length;subloop++){
if (sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[5])
) {
filtermatch=true;
}
}
if(filtermatch && sReg[loop].equalsIgnoreCase(thisFilter[0])
&& sAdmin[loop].equalsIgnoreCase(thisFilter[1])
&& sAmbience[loop].equalsIgnoreCase(thisFilter[3])
&& amountTF >= theTuitionFee;){
list.add(sList[loop]);
}
第二个循环将第二个索引保持在界限内。