为了账号安全,请及时绑定邮箱和手机立即绑定

使用 <a href> 创建顺序链接的 Java 数组

使用 <a href> 创建顺序链接的 Java 数组

四季花海 2021-08-04 16:49:29
以文件名作为数组内容创建文件的代码,文件有前一个词的链接,前一个词和后一个词的链接,目前它创建文件但代码打印!does 迭代数组只是将数组内容打印为数组指定的位置,它应该将数组写入数组内容迭代为 word 前一个链接与前一个数组内容和下一个链接与下一个数组内容public class WrHtmlWithLincs    {    public static void main(String args[]) throws IOException     {     BufferedWriter bw = null;    FileWriter fw = null;             try    {           String word[];        word = new String[14];        word[0] = "Software";        word[1] = "Java";        word[2] = "Android";        word[3] = "Code";        word[4] = "Computer Science";        word[5] = "Satellite Navigation";        word[6] = "Communications";        word[8] = "Calculator";        word[9] = "JavaScript";        word[10] = "Stanford";        word[11] = "Mathematics";                    for(String Ad : word)        {                    try        {        fw = new FileWriter("F:\\" + Ad + ".html");                    bw = new BufferedWriter(fw);        bw.write("<Table align='center' border='4' color = 'cyan'>");            bw.write("<TR>");         bw.write("<TD>");         bw.write("<a href=" + word[y] + ".html>");                                         bw.write("Previous");         bw.write("</a>");          bw.write("</TD>");         bw.write("<TD>");         bw.write(word[y]);         bw.write("</TD>");                  bw.write("<TD>");         bw.write("<a href=" + word[y] + ".html>");                                         bw.write("Next");         bw.write("</a>");        bw.write("</TD>");                     bw.write("</TR>");                    bw.write("<Table>");                                }        catch(IOException d)        {        d.printStackTrace();        }        finally        {        try {            if (bw != null)                bw.close();            if (fw != null)                fw.close();        } catch (IOException d) {            d.printStackTrace();        }        }                  }       }                        catch(Exception d)    {        d.printStackTrace();    }
查看完整描述

1 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

这里有一些对你有用的东西。这是我能想到的最接近简单魔术解决方案的方法。


将此代码粘贴到您正在工作的班级中的某处:


@FunctionalInterface

public interface TriConsumer {


    abstract void consume(Object before, Object current, Object after);


}


public static final forContext(Object[] arr, TriConsumer consumer){

    if (arr == null) {

        return;

    }

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

        consumer.consume(i == 0 ? null : arr[i - 1], arr[i], i == arr.length - 1 ? null : arr[i + 1]);

    }

}

您可以通过执行以下操作来使用此循环:


Word[] wordsArray = //Something


forContext(wordsArray, (previous, current, next) -> {


    //Do what you want with the function.

    System.out.println("The previous word: " + previous);

    System.out.println("The current word: " + current);

    System.out.println("The next word: " + next);


});

编辑:我认为这需要 java 8 或更高版本?


查看完整回答
反对 回复 2021-08-04

添加回答

代码语言

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信