Tester类并不认为我的ArrayList是一个ArrayList

时间:2017-10-17 01:36:01

标签: java arraylist

我试图在Store类中创建一个Item类型的ArrayList,然后在另一个名为StoreTester的类中测试它。我一直收到错误:方法get()未定义类型Store。所以Java认为我的类型为Store的测试对象不是ArrayList,即使我试图这样做。 我的Store类构造函数:

import java.util.ArrayList;
public class Store
{

  private ArrayList<Item> blockbuster;

  public Store(){
    blockbuster = new ArrayList<Item>();
  }

  public void addItem(Item i){
    blockbuster.add(i);
  }
}

这是我的StoreTester类:

import java.util.ArrayList;
public class StoreTester{
  public static void main(String[] args){

    Store videostore = new Store();

System.out.print(videostore.toString());
System.out.print(videostore.get(0));
}
}

由于某种原因,videostore.toString()工作正常并打印出我已添加到其中的对象列表。这是我在Store类中编写的toString方法:

public String toString(){
    String item = "Items in store: " + "\n";
    for(int j = 0; j < blockbuster.size(); j++){
      item = item + blockbuster.get(j).getTitle() + "\n";
    }
    return item;
  } 

但是一旦我尝试获取()索引中的特定对象甚至使用videostore.size(),我得到:方法未定义错误。希望这只是一个语法错误或一些我忽略的简单。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

是的,class BluetoothUtils { companion object { var listener: ListenThread? = null val _UUID = UUID.fromString("a0e7e4c7-0e4e-43b7-9d18-659192512164") val TAG = "BluetoothUtils" val receiver = MainBTStatusReceiver() fun initPairingServer(adapter: BluetoothAdapter){ var mmServerSocket: BluetoothServerSocket? try { var tmp = adapter.listenUsingRfcommWithServiceRecord(TAG, _UUID) mmServerSocket = tmp listener = ListenThread(mmServerSocket) listener!!.start() }catch (ioe: IOException){ Log.e(TAG, "Error initializing Bluetooth", ioe) } } fun cancelListener() = listener!!.cancel() fun connect(adapter: BluetoothAdapter, device: BluetoothDevice){ var btSocket: BluetoothSocket? try { adapter.cancelDiscovery() btSocket = device.createRfcommSocketToServiceRecord(_UUID) PairingThread(btSocket).start() }catch (ioe: IOException){ Log.e(TAG, "error connecting", ioe) } } fun startPair(adapter: BluetoothAdapter, device: BluetoothDevice): Unit{ adapter.cancelDiscovery() Log.d(TAG, device.bondState.toString()) device.createBond() } fun unPair(device: BluetoothDevice): Any = device::class.java.getMethod("removeBond").invoke(device) } } class ListenThread(val btServSock: BluetoothServerSocket) : Thread(){ companion object { val TAG = "ListenThread" } var btSocket: BluetoothSocket? = null override fun run() { super.run() while (true){ try { Log.d(TAG, "listening . . . ") btSocket = btServSock.accept() }catch (ioe: IOException){ Log.e(TAG, "Error", ioe) // SHOULD HANDLE FAILURE OF LISTENER INSTANTIATION break } //manage connection here //with either BluetoothUtils function //or BluetoothSocket extension } } fun cancel() = btServSock.close() } class PairingThread(val btSocket: BluetoothSocket) : Thread(){ companion object { val TAG = "Pairing Thread" } override fun run() { super.run() try { Log.d(TAG, "attempting to connect") btSocket.connect() }catch (ioe: IOException){ Log.e(TAG, "error connecting", ioe) btSocket.close() } } } class MainBTStatusReceiver(): BroadcastReceiver(){ val TAG = "MainBTStatusReceiver" var mAdapter: DeviceAdapter? = null fun setAdapter(adapter: DeviceAdapter){ mAdapter = adapter } override fun onReceive(context: Context?, intent: Intent) { val action = intent.action val devExtra = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice when(action){ BluetoothDevice.ACTION_BOND_STATE_CHANGED -> { val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE) when(device.bondState){ BluetoothDevice.BOND_BONDED -> { Log.d(TAG, "BONDED") } BluetoothDevice.BOND_BONDING -> {Log.d(TAG, "BONDING")} BluetoothDevice.BOND_NONE -> {Log.d(TAG, "NONE")} } } } } 没有方法Store

可以在get中实现

Store

public Item get (int index) { // check for null, then return blockbuster.get(index); } 有一个Store它不是ArrayList是一个Store

正如@ElliottFrisch所提到的,你还需要实现ArrayList方法

size