A fan attending the Nets vs. Mavericks game on Thursday night ended up spending most of his evening in custody ... because he was arrested for throwing a cup of ice that nearly hit Luka Doncic on the court.

According to the NYPD, 24-year-old Justin Boatswain hurled the object during the middle of overtime at Barclays Center in Brooklyn ... as Kevin Durant was trying to sink some free throws.

Video from the game's broadcast shows the cup landed about six feet from the Mavericks' best player -- startling Doncic as ice splattered everywhere.