简体   繁体   English

我如何引用我的列表中排队的对象的某个参数?

[英]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.

相关问题 如何正确将List转换为我的Object类型的ArrayList? - How do I properly convert List to ArrayList of my type of Object? 我如何在数据库构造函数中设置参数,因为它需要接收消息列表? - How do I set my parameter in my Database constructor as it takes in a list of Messages? 如何在我的自定义对象中正确实现领域列表? - How do I properly implement a Realm List into my custom object? 如何从Web应用程序的根目录引用Java Web应用程序中的任何图像? - How do i refer to any image in my java web application from root of my web application? 如何引用迭代器中的当前对象 - How do I refer to the current object in an iterator 如何参考特定的阵列清单 - How do I refer to specific array list Java:我如何让我的程序确定某个数字是否增加了一定数量,以及是否确定了一个特定的方程式? - Java: How do I have my program determine if a number has went up a certain amount and if so do a particular equation? 我如何指称枚举的分组? 基本的国际象棋游戏,想要有效地引用所有黑白棋子 - How do I refer to the grouping of my enums? Basic chess game, wanting to refer to all black/white pieces efficiently 我如何弄乱我的HttpGet参数编码? - How am I messing up my HttpGet parameter encoding? Streams API :: 如何从列表中修改自定义对象的变量<Object> ? - Streams API :: How do i modify the variables of my custom object from a LIST<Object>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM