热门标签 | 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)。


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 浏览器中的异常检测算法及其在深度学习中的应用
    本文介绍了在浏览器中进行异常检测的算法,包括统计学方法和机器学习方法,并探讨了异常检测在深度学习中的应用。异常检测在金融领域的信用卡欺诈、企业安全领域的非法入侵、IT运维中的设备维护时间点预测等方面具有广泛的应用。通过使用TensorFlow.js进行异常检测,可以实现对单变量和多变量异常的检测。统计学方法通过估计数据的分布概率来计算数据点的异常概率,而机器学习方法则通过训练数据来建立异常检测模型。 ... [详细]
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社区 版权所有