Java 链表 : How to add a Pixel in a Picture

标签 java linked-list

我有一个关于在 LinkedList 中添加“像素”的问题。

有一个黑白图片类(名为“Bild”的类)。 当你创建图片的对象时,你必须说出图片的高度和宽度是多少。宽度 u 还表示图片对象中的像素阵列有多大。所以图片的宽度和数组的长度一样长!

数组中的每个元素应该是图片的列作为“像素”的链表。 但是在链表中只有黑色像素是安全的! 每个“像素”在图片中放置的行都有一个 int。 白色像素不安全!

完成方法:

  • public Pixel pixelAt(int zeile, int spalte) 此方法在“图片”的行/列中返回一个“像素”。 如果没有黑色“像素”,则返回 null。

  • public 无效 printBild() 正在为黑色像素打印“*”,当有白色像素时打印“-”。

未完成方法:

  • public void pixelOn(int zeile, int spalte)

此方法应检查该行/列中是否有黑色像素。 在这里我可以使用 pixelAt() 方法。 一个白色像素应该被改变为一个黑色像素,所以有一个像素被添加到链表中。如果像素已经是黑色,则不应发生任何事情。

在我再次调用 printBild() 之后,白色像素“_”仍然是白色而不是黑色“*”。

...

public class Pixel { 

private int zeile; 
private Pixel next; 

public Pixel ( int zeile, Pixel p ) {
    this.zeile = zeile;
    this.next = p;
}

public int getZeile() {
    return zeile;
}

public Pixel getNext() {
    return next;
}

// Setze Verweis auf nächsten schwarzen Pixel 
public void setNext( Pixel p) {
    next = p;
}

}


public class Bild {

private Pixel [] data;
private int hoehe, breite;  

public Bild (int hoehe, int breite) {
    this.data = new Pixel [breite];

    this.hoehe = hoehe;
    this.breite = breite;
}

public void setData(Pixel [] temp) {
        this.data = temp;   
}

public Pixel pixelAt(int zeile, int spalte) { 

    Pixel iterator = data[spalte];
    if(iterator.getZeile() == zeile) {
        return iterator;
    }
    else {
        while (iterator.getNext() != null) {
            iterator = iterator.getNext();

            if(iterator.getZeile() == zeile) {
                return iterator;
            }
        }
    }
    return null;
}


public void printBild() { 

    String leer = "";
    String [] hi = new String [hoehe];

    for (int k = 0; k < hoehe; k++) {
        hi[k] = leer;
    }

    for (int i = 0; i < data.length; i++) {

        Pixel iterator = data[i];

        for (int j = 0; j < hoehe ; j++) {

            if (iterator == null) {
                hi[j] += " - ";
            }

            else {

            if (iterator.getZeile() == j) {
                hi[j] += " * ";
                iterator = iterator.getNext();
            }
            else {
                hi[j] += " - ";
            }
        }
        }
    }

    for (int q = 0; q < hi.length; q++) {
        System.out.println(hi[q]);
    }


}

public void pixelOn(int zeile, int spalte) {



    if (pixelAt(zeile, spalte) == null) {
        Pixel it = data[spalte];

        for (int i = 1; i < zeile; i++) {   
            it = it.getNext();
        }

        Pixel tmp = it.getNext();
        Pixel neu = new Pixel(zeile, null);
        it.setNext(neu);
        neu.setNext(tmp);
    }   
}
}




public class TestBild {

public static void main(String[] args) {


Pixel a1 = new Pixel(2, null);


Pixel b1 = new Pixel(0, null);

Pixel b2 = new Pixel(1,null);
b1.setNext(b2);

Pixel b3 = new Pixel (3, null);
b2.setNext(b3);


Pixel c1 = new Pixel(1, null);

Pixel c2 = new Pixel(2,null);
c1.setNext(c2);


Pixel [] temp = new Pixel [3];

temp[0] = a1;
temp[1] = b1;
temp[2] = c1;


Bild BW = new Bild(4,3);
BW.setData(temp);


BW.printBild();
BW.pixelOn(2, 0);
System.out.println();
BW.printBild();


}



}

...

谢谢!

最佳答案

首先,您的 pixelOn 比您编写的更棘手。它应该是这样的:

public void pixelOn(int zeile, int spalte) {
    if (pixelAt(zeile, spalte) == null) {
        Pixel neu = new Pixel(zeile, null);
        Pixel it = data[spalte];
        if(it!=null){
            if(it.getZeile()<zeile){
                Pixel tmp=it.getNext();
                while(tmp!=null && tmp.getZeile()<zeile){
                    it = tmp;
                    tmp = it.getNext();
                }
                it.setNext(neu);
                neu.setNext(tmp);
            }else{
                neu.setNext(it);
                data[spalte] = neu;
            }
        }else{
            //there is nothing in the column for now add it
            data[spalte] = neu;
        }
    }
}

其次,您的测试没有插入到白色位置。 (2, 0)是第2行第0列。那里的元素是黑色的。

关于Java 链表 : How to add a Pixel in a Picture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32123292/

相关文章:

c - 将输入文件标记为链表

java - 如何动态填充注释

java - PKCS11Exception ckr_attribute_type_invalid

javascript - 链表包含函数返回 false,为什么? JavaScript

c - 循环链表中是否需要头节点

java - 如何使用实现特定接口(interface)的对象声明一个 LinkedList?

java - LinkedList 上的递归合并排序

java - Java中的数组声明错误

java - 使用 java System.getProperty ("Import")

java - Java中通过参数解析数据