简体   繁体   English

如何在while循环内初始化数组并在外部定义它?

[英]How do you initialize an array inside a while loop and define it outside?

I need to be able to assign an array to a .txt file so I need to reference the variable "s" outside the while loop. 我需要能够将数组分配给.txt文件,因此我需要在while循环之外引用变量“ s”。 Even after I define and initialize the variable I still get an error when I initialize in the while loop. 即使在定义和初始化变量之后,在while循环中进行初始化时仍然会出现错误。 What am I doing wrong? 我究竟做错了什么?

 package vp.sga_form_generator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;


public class GUI extends JFrame{
    public GUI() throws FileNotFoundException {

        super("SGA Form Creator - Viper Productions");
            setSize(1000,800);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new GridLayout(30, 2));

            //Opens File
            Scanner names = new Scanner(new File("names.txt"));

    //      String name1 = names.next();
            String[] s;
            while(names.hasNext()){
                s = {names.next()};
            }

            JComboBox names1 = new JComboBox(s);
            JComboBox names2 = new JComboBox(s);

            add(names1);
            add(names2);


    }

}
  1. {names.next()}; {names.next()}; is only allowed when you initialize a String[] so you can say something like String[] s = {"bla","bli","blu"} 仅在初始化String []时才被允许,因此您可以说类似String []的内容s = {“ bla”,“ bli”,“ blu”}
  2. The problem is that String[] is not initialized 问题是String []未初始化
  3. You can not init the String with the appropriate size sinze you dont know it. 您无法使用不知道的适当大小初始化字符串。 So String[] is not the structure of choice 所以String []不是选择的结构
  4. If you need to use String[] then you must do generate a second String[] and copy back and forth each time you add one element. 如果需要使用String [],则必须生成另一个String [],并在每次添加一个元素时来回复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在循环内初始化数据字段? - How do you initialize a datafield inside a loop? 如何在while循环外使用声明数组但在while循环中初始化它? - How to I use declare an array outside a while loop but initialize it in the while loop? 如何在while循环外将项目存储到数组中? - How do I store items into array outside the while loop? 在while循环(或for循环)中创建一个数组,然后在外部使用该数组 - creating an array inside a while loop(or for loop) then using that array outside 如何在 While 循环中递增 Integer 并将其转换为字符串? - How do you Increment an Integer inside a While loop and convert it to String? 如何从循环外部访问在嵌套循环中声明或初始化的数组或变量? - How do you access an array or variable declared or initialized in a nested loop from outside of the loop? 在知道长度之前,如何等待初始化数组? - How do you wait to initialize an array until you know the length? java while循环内部或外部循环 - java while loop inside or outside loop 如何在while循环之外,在while循环中,在方法内部始终访问字符串值 - How to access a string value in a while loop, outside of a while loop, all while inside a method 如何构造一个数组并使用循环将其初始化为值 1 到 5? - How do I construct an array and initialize it to the values 1 through 5 using a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM