写一段代码在遍历ArrayList时删除其中的一个元素

使用ArrayList中的remove(int index)方法来删除元素:

public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        //使用ArrayList的remove(int index)来删除
        for(int i = 1;i<list.size();i++) {
            if(list.get(i).equals("a")) {
                list.remove(i);
            }
        }
        System.out.println(list);//结果为:[a, b, c, d]
    }
}

使用Iterator的remover() 方法来删除元素

public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d")

        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()) {
            if(iterator.next().equals("a")) {
                iterator.remove();
            }
        }
        System.out.println(list);//结果为:[b, c, d]
    }
}

两种方式得到的结果却是不同的,使用for循环其实是由于,在删除第一个a元素之后,索引会指向下一个元素,但是在这个之后我们又再一次进行了i++操作,所以造成了中间的“a”元素直接跳过

 

usage: control_robot.py [-h] [--config_path str] [--robot str] [--robot.type {aloha,koch,koch_bimanual,moss,so101,so100,stretch,lekiwi}] [--robot.gripper_open_degree str] [--robot.max_relative_target str] [--robot.ip str] [--robot.port str] [--robot.video_port str] [--robot.cameras str] [--robot.calibration_dir str] [--robot.leader_arms str] [--robot.follower_arms str] [--robot.teleop_keys str] [--robot.mock str] [--control str] [--control.type {calibrate,teleoperate,record,replay,remote_robot}] [--control.arms str] [--control.teleop_time_s str] [--control.single_task str] [--policy str] [--control.policy.type {act,diffusion,pi0,smolvla,tdmpc,vqbet,pi0fast}] [--control.policy.replace_final_stride_with_dilation str] [--control.policy.pre_norm str] [--control.policy.dim_model str] [--control.policy.n_heads str] [--control.policy.dim_feedforward str] [--control.policy.feedforward_activation str] [--control.policy.n_encoder_layers str] [--control.policy.n_decoder_layers str] [--control.policy.use_vae str] [--control.policy.n_vae_encoder_layers str] [--control.policy.temporal_ensemble_coeff str] [--control.policy.kl_weight str] [--control.policy.optimizer_lr_backbone str] [--control.policy.drop_n_last_frames str] [--control.policy.use_separate_rgb_encoder_per_camera str] [--control.policy.down_dims str] [--control.policy.kernel_size str] [--control.policy.n_groups str] [--control.policy.diffusion_step_embed_dim str] [--control.policy.use_film_scale_modulation str] [--control.policy.noise_scheduler_type str] [--control.policy.num_train_timesteps str] [--control.policy.beta_schedule str] [--control.policy.beta_start str] [--control.policy.beta_end str] [--control.policy.prediction_type str] [--control.policy.clip_sample str] [--control.policy.clip_sample_range str] [--control.policy.num_inference_steps str] [--control.policy.do_mask_loss_for_padding str] [--control.policy.scheduler_name str] [--control.policy.attention_implementation str] [--control.policy.num_steps str] [--control.policy.train_expert_only str] [--control.policy.train_state_proj str] [--control.policy.optimizer_grad_clip_norm str] [--control.policy.vlm_model_name str] [--control.policy.load_vlm_weights str] [--control.policy.add_image_special_tokens str] [--control.policy.attention_mode str] [--control.policy.prefix_length str] [--control.policy.pad_language_to str] [--control.policy.num_expert_layers str] [--control.policy.num_vlm_layers str] [--control.policy.self_attn_every_n_layers str] [--control.policy.expert_width_multiplier str] [--control.policy.min_period str] [--control.policy.max_period str] [--control.policy.n_action_repeats str] [--control.policy.horizon str] [--control.policy.image_encoder_hidden_dim str] [--control.policy.state_encoder_hidden_dim str] [--control.policy.latent_dim str] [--control.policy.q_ensemble_size str] [--control.policy.mlp_dim str] [--control.policy.discount str] [--control.policy.use_mpc str] [--control.policy.cem_iterations str] [--control.policy.max_std str] [--control.policy.min_std str] [--control.policy.n_gaussian_samples str] [--control.policy.n_pi_samples str] [--control.policy.uncertainty_regularizer_coeff str] [--control.policy.n_elites str] [--control.policy.elite_weighting_temperature str] [--control.policy.gaussian_mean_momentum str] [--control.policy.max_random_shift_ratio str] [--control.policy.reward_coeff str] [--control.policy.expectile_weight str] [--control.policy.value_coeff str] [--control.policy.consistency_coeff str] [--control.policy.advantage_scaling str] [--control.policy.pi_coeff str] [--control.policy.temporal_decay_coeff str] [--control.policy.target_model_momentum str] [--control.policy.n_action_pred_token str] [--control.policy.action_chunk_size str] [--control.policy.vision_backbone str] [--control.policy.crop_shape str] [--control.policy.crop_is_random str] [--control.policy.pretrained_backbone_weights str] [--control.policy.use_group_norm str] [--control.policy.spatial_softmax_num_keypoints str] [--control.policy.n_vqvae_training_steps str] [--control.policy.vqvae_n_embed str] [--control.policy.vqvae_embedding_dim str] [--control.policy.vqvae_enc_hidden_dim str] [--control.policy.gpt_block_size str] [--control.policy.gpt_input_dim str] [--control.policy.gpt_output_dim str] [--control.policy.gpt_n_layer str] [--control.policy.gpt_n_head str] [--control.policy.gpt_hidden_dim str] [--control.policy.dropout str] [--control.policy.mlp_hidden_dim str] [--control.policy.offset_loss_weight str] [--control.policy.primary_code_loss_weight str] [--control.policy.secondary_code_loss_weight str] [--control.policy.bet_softmax_temperature str] [--control.policy.sequentially_select str] [--control.policy.optimizer_vqvae_lr str] [--control.policy.optimizer_vqvae_weight_decay str] [--control.policy.n_obs_steps str] [--control.policy.normalization_mapping str] [--control.policy.input_features str] [--control.policy.output_features str] [--control.policy.device str] [--control.policy.use_amp str] [--control.policy.chunk_size str] [--control.policy.n_action_steps str] [--control.policy.max_state_dim str] [--control.policy.max_action_dim str] [--control.policy.resize_imgs_with_padding str] [--control.policy.interpolate_like_pi str] [--control.policy.empty_cameras str] [--control.policy.adapt_to_pi_aloha str] [--control.policy.use_delta_joint_actions_aloha str] [--control.policy.tokenizer_max_length str] [--control.policy.proj_width str] [--control.policy.max_decoding_steps str] [--control.policy.fast_skip_tokens str] [--control.policy.max_input_seq_len str] [--control.policy.use_cache str] [--control.policy.freeze_vision_encoder str] [--control.policy.freeze_lm_head str] [--control.policy.optimizer_lr str] [--control.policy.optimizer_betas str] [--control.policy.optimizer_eps str] [--control.policy.optimizer_weight_decay str] [--control.policy.scheduler_warmup_steps str] [--control.policy.scheduler_decay_steps str] [--control.policy.scheduler_decay_lr str] [--control.policy.checkpoint_path str] [--control.policy.padding_side str] [--control.policy.precision str] [--control.policy.grad_clip_norm str] [--control.policy.relaxed_action_decoding str] [--control.warmup_time_s str] [--control.episode_time_s str] [--control.reset_time_s str] [--control.num_episodes str] [--control.video str] [--control.push_to_hub str] [--control.private str] [--control.tags str] [--control.num_image_writer_processes str] [--control.num_image_writer_threads_per_camera str] [--control.resume str] [--control.repo_id str] [--control.episode str] [--control.root str] [--control.fps str] [--control.play_sounds str] [--control.log_interval str] [--control.display_data str] [--control.viewer_ip str] [--control.viewer_port str] control_robot.py: error: unrecognized arguments: --control.local_files_only=true
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值