java中定義數組
Ⅰ java中數組的定義
java中數組的定義:
字元串數組:
1、String[] str=new String[5];
2、String[] str=new String[]{"a","b"};
3、String[] str={"a","b"};
整形數組:
1、int[] i=new int[5];
2、int[] i=new int[]{0,1};
3、int[] i={0,1};
其他數組定回義的方法類似!答
Ⅱ java中定義數組變數
按照你說的,那就是二維數組啦.int [][] a ,一維數組a[]的變數也是數組
Ⅲ Java 如何定義這樣的數組
//方法四:(使用原生JDK)
importjava.awt.BorderLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.script.ScriptEngine;
importjavax.script.ScriptEngineManager;
importjavax.script.ScriptException;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importsun.org.mozilla.javascript.internal.Decompiler;
importsun.org.mozilla.javascript.internal.IdScriptableObject;
importsun.org.mozilla.javascript.internal.NativeArray;
importsun.org.mozilla.javascript.internal.NativeFunction;
importsun.org.mozilla.javascript.internal.NativeObject;
importsun.org.mozilla.javascript.internal.UintMap;
importsun.org.mozilla.javascript.internal.Undefined;
publicclassMysonextendsJFrame
{
=1L;
privatestaticfinalStringLINE=System.getProperty("line.separator");
privateJTextArealeft=newJTextArea();
privateJTextArearight=newJTextArea();
publicMyson()
{
setTitle("解析JSON");
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JButtonbtn=newJButton("解析");
ScriptEngineManagersem=newScriptEngineManager();
finalScriptEnginese=sem.getEngineByName("js");
btn.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
right.setText("");
try
{
Stringjson=left.getText();
Objectobj=se.eval("("+json+")");
formatJSON(obj,0,"","",false,true,false,false);
}
catch(ScriptExceptione1)
{
Stringexp=e1.toString().replaceAll(".*\:(.*)","$1").replaceAll("\(.+(?=at)","");
right.append(exp);
}
}
});
setLayout(newBorderLayout());
JPanelpanel=newJPanel();
panel.setLayout(newGridLayout(1,2));
left.setLineWrap(true);
right.setLineWrap(true);
JScrollPanep1=newJScrollPane(left);
JScrollPanep2=newJScrollPane(right);
panel.add(p1);
panel.add(p2);
add(panel,BorderLayout.CENTER);
add(btn,BorderLayout.SOUTH);
setVisible(true);
}
privatevoidformatJSON(Objectobj,intlevel,Stringk,Stringv,booleanisObject,booleanisLast,
booleanisDeletedSpace,
booleanisDeleteAndCont)
{
if(!isDeletedSpace)
{
for(inti=0;i<level;i++)
{
right.append(" ");
}
}
if(objinstanceofNativeObject)
{
if(!isDeletedSpace)
{
Stringtemp="";
for(inti=0;i<level;i++)
{
temp+=" ";
}
Stringx=isObject?"""+k+"":"+LINE+temp+"{":"{";
right.append(x+LINE);
}
else
{
Stringx="";
if(!isDeleteAndCont)
{
x=isObject?"""+k+"":{":"{";
}
else
{
x=isObject?"\""+k+"\":{":"{";
}
right.append(x);
}
Object[]ids=((IdScriptableObject)(obj)).getIds();
intlen=ids.length;
intlev=level+1;
for(inti=0;i<len;i++)
{
Stringkey=ids[i].toString();
Objectobject=((IdScriptableObject)(obj)).get(key,null);
if(objectinstanceofUndefined)
{
object="undefined";
}
elseif(null==object)
{
object="null";
}
elseif()
{
object=((NativeFunction)(object)).getEncodedSource();
object=Decompiler.decompile(object.toString(),-1,newUintMap());
object=object.toString().replaceAll("^\((.*)\)$","$1}");
}
if(key.matches("^[\d+\.]+$"))
{
object=((IdScriptableObject)(obj)).get(Integer.parseInt(key),null);
}
Stringvalue=object.toString().replaceAll("^(.+)\.0$","$1").replaceAll(""","'");
formatJSON(object,lev,key,value,true,i==len-1,isDeletedSpace,isDeleteAndCont);
}
if(!isDeletedSpace)
{
for(inti=0;i<level;i++)
{
right.append(" ");
}
if(isLast)
{
right.append("}"+LINE);
}
else
{
right.append("},"+LINE);
}
}
else
{
if(isLast)
{
right.append("}");
}
else
{
right.append("},");
}
}
isObject=true;
}
elseif(objinstanceofNativeArray)
{
if(!isDeletedSpace)
{
Stringtemp="";
for(inti=0;i<level;i++)
{
temp+=" ";
}
Stringx=isObject?"""+k+"":"+LINE+temp+"[":"[";
right.append(x+LINE);
}
else
{
Stringx="";
if(!isDeleteAndCont)
{
x=isObject?"""+k+"":[":"[";
}
else
{
x=isObject?"\""+k+"\":[":"[";
}
right.append(x);
}
Object[]ids=((IdScriptableObject)(obj)).getIds();
intlen=ids.length;
intlev=level+1;
for(inti=0;i<len;i++)
{
IdScriptableObjectio=(IdScriptableObject)(obj);
Objectobject=io.get(i,null);
if(objectinstanceofUndefined)
{
object="undefined";
}
elseif(null==object)
{
object="null";
}
elseif()
{
object=((NativeFunction)(object)).getEncodedSource();
object=Decompiler.decompile(object.toString(),-1,newUintMap());
object=object.toString().replaceAll("^\((.*)\)$","$1}");
}
Stringvalue=object.toString().replaceAll("^(.+)\.0$","$1").replaceAll(""","'");
formatJSON(object,lev,i+"",value,false,i==len-1,isDeletedSpace,isDeleteAndCont);
}
if(!isDeletedSpace)
{
for(inti=0;i<level;i++)
{
right.append(" ");
}
if(isLast)
{
right.append("]"+LINE);
}
else
{
right.append("],"+LINE);
}
}
else
{
if(isLast)
{
right.append("]");
}
else
{
right.append("],");
}
}
isObject=false;
}
else
{
v=v.replaceAll("("|\\)","\\$1");
Stringvv="";
if(!isDeleteAndCont)
{
vv=isObject?("""+k+"":""+v+"""):("""+v+""");
}
else
{
vv=isObject?("\""+k+"\":\""+v+"\""):("\""+v+"\"");
}
vv=isLast?vv:vv+",";
if(!isDeletedSpace)
{
right.append(vv+LINE);
}
else
{
right.append(vv);
}
}
}
publicstaticvoidmain(String[]args)
{
newMyson();
}
}
Ⅳ java 在類中如何定義數組
java里一般用collection介面來替代使用數組。。。。。
你可以看看list,set,map的collection介面部分。
希望對你能夠有所幫助。。。。。
Ⅳ java定義數組
java中數組的定義:
字元串數組:
1、String[] str=new String[5];
2、String[] str=new String[]{"a","b"};
3、String[] str={"a","b"};
整形數組:
1、int[] i=new int[5];
2、int[] i=new int[]{0,1};
3、int[] i={0,1};
其他內數組定義的方法容類似!
Ⅵ JAVA中如何定義數組類型的數組
你想幹啥。二維數組?它的元素就是一位數組。int[][] arr = new int[3][2];裡面有3個一位數組,每個一位數組有2個元素
Ⅶ 怎樣用java定義一個int數組
int[] anArray = null;/*** 這樣賦值在java中是不允許的,會判定為語法錯誤
* 首先java只可以在class中【聲明】成員變數,而不能對變數賦值,
* 你只能在方法中(例如:main方法或者其他方法)對變數賦值。** 但是class中允許在聲明變數的同時,為變數賦【初始值】。
* 也就是說 int[] anArray2 = new int[10]; 這樣是允許的
* 這里的 new int[10] 是初始化操作(不是賦值操作)。
* *///anArray = new int[10];
static int[] bArray = null;
public void setValue(){
anArray = new int[10]; //這里是可以的,方法內可以賦值。}public static void main(String[] args){
//anArray = new int[10];/*** 這里是不允許的,因為主方法(即main方法)是靜態的(由static修飾)
* 所以如果是靜態的成員變數可以賦值,例如 bArray
* 下面的兩種方式都是可以賦值的* */bArray = new int[10];
bArray = new int[]{1,2,2,1,1,1,1,1,1,1};}}<b答案補充</b
static修飾的方法,只可以調用static修飾的成員變數
所以在main方法中為【非靜態】的anArray數組賦值是不允許的。
另外bArray = new int[]{1,2,2,1,1,1,1,1,1,1};是為數組賦值的方式。
Ⅷ java中數組定義是什麼
java中數組的定義:
字元串數組:
1、String[] str=new String[5];
2、String[] str=new String[]{"a","b"};
3、String[] str={"a","b"};
整形數組:
1、int[] i=new int[5];
2、int[] i=new int[]{0,1};
3、int[] i={0,1};
其他回數組定義的方法類似!答
Ⅸ java中如何定義數組
int[] anArray = null;這樣賦值在中是不允許的,會判定為語法錯誤,首先java只可以在class中聲明成員變數,而不能對變數賦值,你只能在方法中(例如:main方法或者其他方法)對變數賦值。但是class中允許在聲明變數的同時,為變數賦初始值。
也就是說 int anArray2 = new int[10]; 這樣是允許的,這里的 new int[10] 是初始化操作(不是賦值操作)。
anArray = new int[10];
static int[] bArray = null;
public void setValue()
{anArray = new int[10]; }
public static void main(String[] args)
{anArray = new int[10]; }
java中使用 [][] 來定義二維數組 定義數組時也可同時初始化
下面是一些例子
float[][] numthree; //定義一個float類型的2維數組
numthree=new float[5][5]; //為它分配5行5列的空間大小
numthree[0][0]=1.1f; //通過下標索引去訪問 1行1列=1.1
long[][] numfive=new long[5][]; //定義一個long類型的不規則數組
numfive[0]=new long[5]; //為第1行分配5列
double型的數組分配3行3列的空間同時賦值
double[][] numsix={{1.111D,2.222D,3.333D},{4.444D,5.555D,6.666D}};
int[][] numseven=new int[][]{{10,20,30},{40,50},{60}};
Ⅹ 在java中如何定義一個字元串數組
1. java中定義一個字元串數組方式如下,string類型和其他基本類型相似,創建數組有兩種方式:
String[] str={"AAA","BBB","CCC"};
String str[]={"AAA","BBB","CCC"};
2.推薦用ArrayList<String> strArray = new ArrayList<String> (); 比較靈活。
3.也可以寫為如下格式:class[] array; array = new class[number];其中前半句為聲明,後半句為初始化,初始化必須要讓編譯器知道大小,聲明的時候java是不分配內存的,只有創建的時候也就是new的時候才會分配內存。
(10)java中定義數組擴展閱讀:
1.數組是相同數據類型的元素的集合。
2.數組中的各元素的存儲是有先後順序的,它們在內存中按照這個先後順序連續存放在一起。
3.數組元素用整個數組的名字和它自己在數組中的順序位置來表示。例如,a[0]表示名字為a的數組中的第一個元素,a[1]代表數組a的第二個元素,以此類推。
4.對於VB的數組,表示數組元素時應注意:下標要緊跟在數組名後,而且用圓括弧括起來(不能用其他括弧)。下標可以是常量,變數,或表達式,但其值必須是整數。下標必須為一段連續的整數,其最小值成為下界,其最大值成為上界。不加說明時下界值默認為1。