如何打印数组列表的数组

时间:2017-06-27 06:24:36

标签: java arrays arraylist

I have 2 -d array p[i][j] by using 

    for (int jj=0; jj < N;jj++){

            My_0[jj] = p[0][jj]; 
            My_1[jj] = p[1][jj];
            My_2[jj] = p[2][jj]; 
            My_3[jj] = p[3][jj]; 
            My_4[jj] = p[4][jj];
            My_5[jj] = p[5][jj]; 
            My_6[jj] = p[6][jj]; 
            My_7[jj] = p[7][jj];
            My_8[jj] = p[8][jj];
            My_9[jj] = p[9][jj]; 

    }
I extracted rows from p and inserted into My_0 to My_9 one by one,,,then I inserted each My_0 to My_9 into a arraylist class object 

    List<AA> part = new ArrayList<AA>();
    AA newP = new AA(); 

    // using
    newP.setO(My_0);
    newP.setO(My_1);
    newP.setO(My_2);
    newP.setO(My_3);
    newP.setO(My_4);
    newP.setO(My_5);
    newP.setO(My_6);
    newP.setO(My_7);
    newP.setO(My_8);
    newP.setO(My_9);

    part.add(newP);
    // then print this array list using

    System.out.println("Size === "+ part.size());
    for (int j1=0; j1 < part.size();j1++) {
        System.out.println("" + Arrays.toString((part).get(j1).getO()));
    }

但问题是它只将my_9数组打印到part.size()。我应该怎么做才能将所有My_0打印到My_9。

           newP0.setO(My_0);
           newP1.setO(My_1); //like this done for every My_0 to My_9
           part.add(newP0);
           part.add(newP1); //like this done for every My_0 to My_9

         System.out.println("Size === "+ part.size());
          for (int j1=0; j1 < part.size();j1++){
          System.out.println("" + Arrays.toString((part).get(j1).getO()));

我正在使用上面的代码打印部分它将打印尺寸= 100并打印My_0到My_9值10次,是否正确或打印时代码出错,我认为它应该只打印My_0到My_9价值仅一次

1 个答案:

答案 0 :(得分:1)

只打印My_9,因为您设置的newP.setO(My_..)始终覆盖O之前的值。

newP.setO(My_9)是您在newP上执行的最后一组,这意味着O具有My_9值。

// using
newP.setO(My_0); // o is now equal to My_0
newP.setO(My_1); // o is now equal to My_1
newP.setO(My_2); // o is now equal to My_2 
... 

也许你要做的就是创建一个新的AA对象,给它你想要的My_xx,将它添加到part,然后再次为My_.. AA newP0 = new AA(); newP0.setO(My_0); part.add(newP0); AA newP1 = new AA(); newP1.setO(My_1); part.add(newP1); 1}}你有。

像这样:

Arrays.toString((part).get(ii)

另外,正如评论中所述,您正在进行j1,但您已在for循环中声明for (int j1=0; j1 < parti.size();j1++) { System.out.println("" + Arrays.toString((part).get(ii).getO())); }

parti

您还会在for循环声明中看到part应为AA

说实话,我不知道你想用这个来实现什么。我们缺少很多信息,例如@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_peripheral); getActionBar().setDisplayHomeAsUpEnabled(true);//on this line i am getting error mListViewHeader = (View) getLayoutInflater().inflate(R.layout.peripheral_list_services_header, null, false); connectViewsVariables(); final Intent intent = getIntent(); mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME); mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS); mDeviceRSSI = intent.getIntExtra(EXTRAS_DEVICE_RSSI, 0) + " db"; mDeviceNameView.setText(mDeviceName); mDeviceAddressView.setText(mDeviceAddress); mDeviceRssiView.setText(mDeviceRSSI); getActionBar().setTitle(mDeviceName); // mListView.addHeaderView(mListViewHeader); // mListView.setOnItemClickListener(listClickListener); } ?您应该考虑使用Camel casing作为变量。