热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在类中定义数组时出错-Errorondefiningarraysinclass

Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra

I am trying to make a class that will read a text file of names into an array, then return that array to the main class. However I am having an error when attempting to define the arrays.

我试图创建一个类,将一个名称的文本文件读入一个数组,然后将该数组返回到主类。但是,我在尝试定义数组时遇到错误。

public class Test{
String[] foo;
String[] zoo;
String[] yoo;
}

I get an error on String[] yoo

我在String [] yoo上收到错误

Syntax error on token ";", { expected after this 
token

I really have no clue what is going on, can anyone help?

我真的不知道发生了什么,有人可以帮忙吗?

Edit - Actual section of code

编辑 - 代码的实际部分

    String[] swords;
    String[] prefix;
    String[] suffix;
    String[] rarity;
    String[] colors = {"2","3","4","5","6","7","9","a","b","c","d","e","f"};
    String[] bows = new String[3];
    String[] enchantments = {"Frost","Igniton","Projection","Explosion","Enhance Jump","Enhance Speed","Resist Flames","Invisibility"};
    rarity = new String[1000];
    swords = new String[1000];
    bows = new String[1000];
    prefix = new String[1000];
    suffix = new String[1000];

4 个解决方案

#1


1  

You should not initialize like this in outside the constructors or methods

您不应该在构造函数或方法之外进行这样的初始化

Wrong:

public Test{
 String[] rarity;
 String[] swords;
 rarity = new String[1000]; 
 swords = new String[1000];
}

You can do this

你可以这样做

public Test{
      String[] rarity = new String[1000]; 
      String[] swords = new String[1000];
    }

if the variables are static you can use static block

如果变量是静态的,则可以使用静态块

public Test{
   private static int x;
   static{
          x=2;
   }

}

Use constructor instead to initialize:

使用构造函数来初始化:

 public Test{
    String[] swords;
    String[] prefix;
    String[] suffix;
    String[] rarity;
    String[] colors = {"2","3","4","5","6","7","9","a","b","c","d","e","f"};
    String[] bows = new String[3];
    String[] enchantments = {"Frost","Igniton","Projection","Explosion","Enhance Jump","Enhance Speed","Resist Flames","Invisibility"};
  public Test(){
    rarity = new String[1000];
    swords = new String[1000];
    bows = new String[1000];
    prefix = new String[1000];
    suffix = new String[1000];
  }
}

That's all

#2


2  

You can't assign values to fields outside of the field declaration or a block (or constructor). So this line

您不能将值分配给字段声明或块(或构造函数)之外的字段。所以这一行

rarity = new String[1000];

(and the other similar ones) should be in the constructor, or the declaration should also initialize the field:

(以及其他类似的)应该在构造函数中,或者声明也应该初始化字段:

String[] rarity = new String[1000];

#3


1  

unless you post all of your code it is not possible to be sure the answer is correct.

除非您发布所有代码,否则无法确定答案是否正确。

but I guess you have this:

但我想你有这个:

rarity = new String[1000];
swords = new String[1000];
bows = new String[1000];
prefix = new String[1000];
suffix = new String[1000];

outside a method. that is not possible in Java.

在方法之外。这在Java中是不可能的。

do like this instead:

改为:

String[] rarity = new String[1000];

or init the field inside a method/constructor

或者在方法/构造函数中初始化字段

#4


0  

First of all, you should make them public or private(unless you really need it to be package-private).

首先,你应该将它们公开或私有(除非你真的需要它是包私有的)。

An array is created like this: Type[] variableName = new Type[length];

像这样创建一个数组:Type [] variableName = new Type [length];

length is the size of the array, for example String[] test = new String[5] can contain 5 strings. To set them use test[i] = someString; where i is the index(starting at 0 and ending at length - 1).

length是数组的大小,例如String [] test = new String [5]可以包含5个字符串。设置它们使用test [i] = someString;其中i是索引(从0开始,以长度结束 - 1)。

You can also make an ArrayList if you do not want your array to be limited, but that uses a bit more memory.

如果您不希望限制数组,但是使用更多内存,也可以创建一个ArrayList。

ArrayList variableName = new ArrayList<>();

ArrayList variableName = new ArrayList <>();

For example: ArrayList test = new ArrayList<>();

例如:ArrayList test = new ArrayList <>();

To add to it use test.add(someString) and to get: arrayList.get(i) where i is the index.

要添加到它使用test.add(someString)并获取:arrayList.get(i)其中i是索引。

A disadvantage of ArrayList is that primitive types(int, byte, boolean, ...) cannot be used. You'll need to use Integer, Byte, Boolean, ...

ArrayList的一个缺点是不能使用原始类型(int,byte,boolean,...)。你需要使用Integer,Byte,Boolean,...

If you have an ArrayList, you could intArrayList.add(5) because autoboxing transforms 5 into new Integer(5).

如果您有一个ArrayList ,则可以使用intArrayList.add(5),因为autoboxing将5转换为新的Integer(5)。


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍了如何使用 Spring Boot DevTools 实现应用程序在开发过程中自动重启。这一特性显著提高了开发效率,特别是在集成开发环境(IDE)中工作时,能够提供快速的反馈循环。默认情况下,DevTools 会监控类路径上的文件变化,并根据需要触发应用重启。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
author-avatar
百度地震姜常宏
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有