java-multilpe数组初始化

时间:2017-08-13 06:50:16

标签: java arrays

好吧,我正在尝试使用此方法初始化多个数组,它给出了我提到的错误:任何解决方案

            int [] x,y,z=new int [10];
            System.out.println("The first element of x:"+x[0]);
            System.out.println("The second element of y:"+y[1]);
            System.out.println("The third element of z:"+z[2]);`

错误是:

ex1.java:47: error: variable x might not have been initialized
    System.out.println("The first element of x:"+x[0]);
                                                 ^
ex1.java:48: error: variable y might not have been initialized
    System.out.println("The second element of y:"+y[1]);
                                                  ^
2 errors

2 个答案:

答案 0 :(得分:3)

int [] x,y,z=new int [10];

相当于:

int[] x;
int[] y;
int[] z=new int [10];

xy未初始化。

你应该初始化所有3个数组:

int[] x = new int [10];
int[] y = new int [10];
int[] z = new int [10];

答案 1 :(得分:1)

Log.d("PIECHART dataset", "addDataSet started");
ArrayList yEntrys = new ArrayList<>();
ArrayList xEntrys = new ArrayList<>();

for(int i = 0; i < yData.length; i++){
    if(yData[i]>0) {

    switch(i){
        case 0:
            yEntrys.add(new PieEntry(yData[i], "banana"));
            break;
        case 1:
            yEntrys.add(new PieEntry(yData[i], "guava"));
            break;
        case 2:
            yEntrys.add(new PieEntry(yData[i], "apple"));
            break;
        case 3:
            yEntrys.add(new PieEntry(yData[i], "pineapple"));
            break;
        case 4:
            yEntrys.add(new PieEntry(yData[i], "mango"));
            break;
        case 5:
            yEntrys.add(new PieEntry(yData[i], "papaya"));
            break;
        case 6:
            yEntrys.add(new PieEntry(yData[i], "dates"));
            break;
        default : ;
    }

    }
}
//create the data set

pieDataSet = new PieDataSet(yEntrys, "");
pieDataSet.setSliceSpace(2f);
pieDataSet.setValueTextSize(15f);
pieDataSet.setValueTextColor(Color.RED);/* this line not working */
pieDataSet.setSelectionShift(10f);
pieDataSet.setValueLinePart1OffsetPercentage(80.f);
pieDataSet.setValueLinePart1Length(1f);
pieDataSet.setValueLinePart2Length(0.9f);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

//add colors to dataset
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.rgb(156,254,230));
colors.add(Color.rgb(159,185,235));
colors.add(Color.rgb(143,231,161));
colors.add(Color.rgb(160,239,136));
colors.add(Color.rgb(200,246,139));
colors.add(Color.rgb(176,219,233));
colors.add(Color.rgb(183,176,253));

pieDataSet.setColors(colors);

///// disabling chart legend
pieChart.getLegend().setEnabled(false);

Log.d("PIECHART dataset2", xEntrys.size()+" ");

//create pie data object
PieData pieData = new PieData(pieDataSet);
pieData.setValueTextColor(Color.RED);/* only YValue color changes, Xvalues         remains white*/
pieData.setValueFormatter(new valueFormat());
pieChart.setData(pieData);
//pieDataSet.notifyDataSetChanged();
//pieChart.invalidate();

您只是在此行中初始化了int [] x,y,z=new int [10]; ,而不是zx。您还需要为其他两个变量添加其他初始化。

y