[英]How do I refer to a certain parameter of my Object that is queued up in my list?
How do I refer to a certain parameter of my Object that is queued up in my list?我如何引用我的列表中排队的对象的某个参数? We're currently working with lists and Im trying to compare a parameter with a specific parameter of an Object that is in my list.
我们目前正在处理列表,我正在尝试将参数与列表中对象的特定参数进行比较。 The class list only offers
getContent()
, but I don't need the object I need the parameter inside the object.类列表只提供
getContent()
,但我不需要对象我需要对象内部的参数。
if (answer.equals(a)) {
vocList.getContent(Vocabulary.getGerman());
}
I've got a getter in the class Vocabulary.我在词汇课上有一个吸气剂。 Vocabulary is queued in my List
词汇在我的列表中排队
Vocabulary class:词汇课:
public class Vocabulary {
private String deutsch;
private String englisch;
public Vocabulary(String pGerman, String pEnglish)
{
english = pEnglish;
german = pGerman;
}
public String getGerman()
{
return german;
}
public String getEnglish()
{
return english;
}
}
To get the value of an object you need the getter you created in the Vocable class:要获取对象的值,您需要在 Vocable 类中创建的 getter:
vocList.get(i).getGerman(); //i is the index of the object in your list.
So your Vocable class should look like this to actually store the values you create the objects with:所以你的 Vocable 类应该看起来像这样来实际存储你创建对象的值:
public class Vocable{
private String german;
private String english;
public Vocable(String pGerman, String pEnglish){
english = pEnglish;
german = pGerman;
}
public String getGerman(){
return german;
}
public String getEnglish(){
return english;
}
}
This appears to be a homework question since you say, " We are currently working with lists."这似乎是一个家庭作业问题,因为您说:“我们目前正在处理列表。” So, I'll try to steer you in the right direction without giving the answers away.
所以,我会尽量引导你朝着正确的方向前进,而不会给出答案。 I also recommend you read the Oracle Java Tutorial on Objects .
我还建议您阅读Oracle Java 对象教程。
Now, to your question: You want to get a handle to the specific Object---once you have a handle for the Object, you can call its instance methods or access its fields' values.现在,对于您的问题:您想获得特定对象的句柄---一旦您拥有对象的句柄,您就可以调用其实例方法或访问其字段的值。 For example, suppose I have a
Rectangle
object:例如,假设我有一个
Rectangle
对象:
public class Rectangle {
public int x;
public int y;
public int width;
public int height;
public Rectangle(int x, int y, int width, int height) {
this.X = x;
this.Y = y;
this.width = width;
this.height = height;
}
}
You can use the following code to print create a new Rectangle
and then print its fields' values.您可以使用以下代码打印创建一个新的
Rectangle
,然后打印其字段的值。 Notice you have to get the element from the list (using get(0)
) before I could access the Rectangle
's variables.请注意,您必须先从列表中获取元素(使用
get(0)
),然后才能访问Rectangle
的变量。 You have to do the same thing in your code, but it looks like you're using a custom List
implementation;您必须在代码中做同样的事情,但看起来您正在使用自定义
List
实现; So, you'll have to use the getContent
method to get a reference to the object.因此,您必须使用
getContent
方法来获取对对象的引用。
public class CreateObjectDemo {
public static void main(String[] args) {
//create an ArrayList
static List<Rectangle> rectangleList = new ArrayList<Rectangle>();
//create a rectangle and add it to the array list
Rectangle rect = new Rectangle(50,50,100,75);
rectangleList.add(rect);
printFirstRectangle(rectangleList);
}
/** Prints the first rectangle in the list */
private static void printFirstRectangle(List<Rectangle> rectangles){
if (rectangles.size() > 0){ //if the list has at least one element
//get the handle for the first element---the one at the 0th index
Rectangle firstRect = rectangle.get(0);
//print the result
System.out.printf("First Rectangle, x=%d, y=%d, width=%d, height=%d\n",
firstRect.x,
firstRect.y,
firstRect.width,
firstRect.height);
} else { //if the list is empty
System.out.println("The list is empty.");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.