我的问题是我有一个创建arraylist的方法。我可以根据需要将其传递给其他方法。但是,当用户点击"计算"时,我想调用方法calculate
。使用以前创建的数组列表的按钮,但我不知道该怎么做。
我最接近的是将我的arraylist声明为全局变量,但我知道全局变量通常都很糟糕,即便如此,我在代码Cannot resolve symbol 'al'
的最后一行得到lookUpMethod(al, lookUpString);
。
我不确定我做错了什么或最好的方法是什么。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String []> al = new ArrayList<>(); //tried global variable
exampleArrayListofArray (al);
}
public void exampleArrayListofArray (List<String []> al) {
//ArrayList<String []> al = new ArrayList<>(); //original array list declaration
al.add(new String[] {"AB","YZ","12"});
al.add(new String[] {"CD","WX", "34"});
al.add(new String[] {"EF","UV", "56"});
al.add(new String[] {"GH","ST", "78"});
al.add(new String[]{"IJ", "QR", "91"});
al.add(new String[]{"KL", "OP", "10"});
displayArrayListofArray(al);
}
public void displayArrayListofArray(List<String[]> al) {
for (String [] row : al)
for (int column = 0; column <= 2 ; column ++){
System.out.println("Value at Index Row " + al.indexOf(row) +
" Column " + column + " is " + (row)[column]);
}
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
lookUpMethod(al, lookUpString);
}
public void lookUpMethod(List<String[]> al, String lookUpString) {
boolean isStringFound = false;
for (String[] row : al) {
for (int column = 0; column <= 2; column++) {
if (al.get(al.indexOf(row))[column].equals(lookUpString)) {
System.out.println("Index of '" + lookUpString + "': " + al.indexOf(row) + column);
isStringFound = true;
}
}
}
if (!isStringFound) {
System.out.println("Search string '" + lookUpString + "' does not exist.");
}
}
public void calculate(View view){
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
System.out.println("user input : " + lookUpString);
lookUpMethod(al, lookUpString);
}
}
答案 0 :(得分:3)
全局变量应该在onCreate之外定义,我认为它通常不好,你可以试试这段代码:
public class MainActivity extends AppCompatActivity {
private ArrayList<String []> al; //global variable in class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
al = new ArrayList<>();
exampleArrayListofArray (al);
}
public void exampleArrayListofArray (List<String []> al) {
//ArrayList<String []> al = new ArrayList<>(); //original array list declaration
al.add(new String[] {"AB","YZ","12"});
al.add(new String[] {"CD","WX", "34"});
al.add(new String[] {"EF","UV", "56"});
al.add(new String[] {"GH","ST", "78"});
al.add(new String[]{"IJ", "QR", "91"});
al.add(new String[]{"KL", "OP", "10"});
displayArrayListofArray(al);
}
public void displayArrayListofArray(List<String[]> al) {
for (String [] row : al)
for (int column = 0; column <= 2 ; column ++){
System.out.println("Value at Index Row " + al.indexOf(row) +
" Column " + column + " is " + (row)[column]);
}
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
lookUpMethod(al, lookUpString);
}
public void lookUpMethod(List<String[]> al, String lookUpString) {
boolean isStringFound = false;
for (String[] row : al) {
for (int column = 0; column <= 2; column++) {
if (al.get(al.indexOf(row))[column].equals(lookUpString)) {
System.out.println("Index of '" + lookUpString + "': " + al.indexOf(row) + column);
isStringFound = true;
}
}
}
if (!isStringFound) {
System.out.println("Search string '" + lookUpString + "' does not exist.");
}
}
public void calculate(View view){
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
System.out.println("user input : " + lookUpString);
lookUpMethod(al, lookUpString);
}
}
答案 1 :(得分:3)
在ArrayList<String []> al = new ArrayList<>();
函数之外声明onCreate
。
我真的不知道你是如何遇到这种问题的,因为现在所有的功能都会看到你的变量。作为清理,您可以在调用函数时删除变量al
,因为不再需要它,因为每个函数现在都可以看到它。如果我没弄错的话,你的最终代码如下:
public class MainActivity extends AppCompatActivity {
ArrayList<String []> al = new ArrayList<>(); //declare it here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exampleArrayListofArray ();
}
public void exampleArrayListofArray () {
al.add(new String[] {"AB","YZ","12"});
al.add(new String[] {"CD","WX", "34"});
al.add(new String[] {"EF","UV", "56"});
al.add(new String[] {"GH","ST", "78"});
al.add(new String[]{"IJ", "QR", "91"});
al.add(new String[]{"KL", "OP", "10"});
displayArrayListofArray();
}
public void displayArrayListofArray() {
for (String [] row : al)
for (int column = 0; column <= 2 ; column ++){
System.out.println("Value at Index Row " + al.indexOf(row) +
" Column " + column + " is " + (row)[column]);
}
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
lookUpMethod(lookUpString);
}
public void lookUpMethod(String lookUpString) {
boolean isStringFound = false;
for (String[] row : al) {
for (int column = 0; column <= 2; column++) {
if (al.get(al.indexOf(row))[column].equals(lookUpString)) {
System.out.println("Index of '" + lookUpString + "': " + al.indexOf(row) + column);
isStringFound = true;
}
}
}
if (!isStringFound) {
System.out.println("Search string '" + lookUpString + "' does not exist.");
}
}
public void calculate(View view){
EditText userField = (EditText) findViewById(R.id.user_field);
String lookUpString = userField.getText().toString();
System.out.println("user input : " + lookUpString);
lookUpMethod(lookUpString);
}
}
请注意我是如何删除&#34; al&#34;在函数调用和函数参数中。将al
作为通过函数传递的局部变量的问题是,当您点击calculate
按钮时,calculate
函数看不到al
,除非它{&#39} ;一个全局变量。
去尝试修改后的代码。
答案 2 :(得分:2)
您需要将函数外部的arrayList解析为全局变量。
ArrayList al = new ArrayList&lt;&gt;();
答案 3 :(得分:2)
范围:局部变量仅在声明它们的方法或块中可见,而实例变量可以被类中的所有方法看到。声明它们的位置:局部变量在方法或块内声明,而实例变量在类中但在方法外部声明。
你需要声明
ArrayList<String []> al = new ArrayList<>();
在onCreate函数之外。