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


推荐阅读
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 使用Flutternewintegration_test进行示例集成测试?回答首先在dev下的p ... [详细]
  • Android中怎么利用TextView显示部分文字高亮
    这篇文章将为大家详细讲解有关Android中怎么利用TextView显示部分文字高亮,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇 ... [详细]
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社区 版权所有