初始化嵌套对象java

时间:2016-03-15 19:11:54

标签: java android arraylist

如果我初始化包含LatLng对象的ArrayLists的ArrayList,LatLngs的ArrayLists是否设置为null或为空?我问的原因是因为当我检查以确保第一个ArrayList为null时,我得到一个IndexOutofBoundsException(size = 0,index = 0)。这是否意味着当我第一次初始化对象时,所有对象都已设置为空并且不为null?所以我不必每次都初始化一个新的LatLng对象,也不必初始化每个嵌套的ArrayList?

我的初始化对象:

# Importing sql types
from pyspark.sql.types import StringType, StructType, StructField
from pyspark.sql.functions import col

# Building a simple dataframe:
schema = StructType([
             StructField("id name", StringType(), True),
             StructField("cities venezuela", StringType(), True)
         ])

column1 = ['A', 'A', 'B', 'B', 'C', 'B']
column2 = ['Maracaibo', 'Valencia', 'Caracas', 'Barcelona', 'Barquisimeto', 'Merida']

# Dataframe:
df = sqlContext.createDataFrame(list(zip(column1, column2)), schema=schema)
df.show()

exprs = [col(column).alias(column.replace(' ', '_')) for column in df.columns]
df.select(*exprs).show()

我得到例外的地方:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);


    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    if (savedInstanceState == null) {
        retainedFragment = new RetainedFragment();
        getFragmentManager().beginTransaction().add(retainedFragment, RetainedFragment.class.toString()).commit();
        handleConnection = new HandleConnection(this, handler);
        retainedFragment.setHandleConnection(handleConnection);

    } else {

        remoteFragment = (RemoteFragment) getFragmentManager().findFragmentByTag(RemoteFragment.class.toString());
        retainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag(RetainedFragment.class.toString());

        if (remoteFragment == null)
            remoteFragment = new RemoteFragment();

        handleConnection = retainedFragment.getHandleConnection();
        remoteFragment.setHandleConnection(handleConnection);
    }

    fragmentTransaction.commit();

}

1 个答案:

答案 0 :(得分:0)

你得到数组越界错误的原因是因为你没有在该索引处存储任何东西,主要是因为你没有在ArrayList中插入任何东西,所以大小为0。因此,在执行get调用之前,请检查ArrayList的大小以防止IndexOutOfBoundException。因此,在badSpots.size()>0之前检查!badSports.isEmpty()badSpots.get(0)

所以你仍然需要初始化你存储的对象。不,您的LatLng对象不会被初始化。通用只确保Type LatLng对象存储在ArrayList中。