我已经创建了一个我的问题的简短例子.我正在匿名创建一个对象列表并将它们添加到一个ArrayList.一旦项目进入,ArrayList我稍后回来并向列表中的每个对象添加更多信息.如果你不知道它的索引,有没有办法从列表中提取特定对象?
我只知道对象的'名字',但你不能做list.get(ObjectName)任何事情.建议的方法是什么?每次我想要检索一个特定对象时,我宁愿不必遍历整个列表.
public class TestCode{
public static void main (String args []) {
Cave cave = new Cave();
// Loop adds several Parties to the cave's party list
cave.parties.add(new Party("FirstParty")); // all anonymously added
cave.parties.add(new Party("SecondParty"));
cave.parties.add(new Party("ThirdParty"));
// How do I go about setting the 'index' value of SecondParty for example?
}
}
class Cave {
ArrayList<Party> parties = new ArrayList<Party>();
}
class Party extends CaveElement{
int index;
public Party(String n){
name = …Run Code Online (Sandbox Code Playgroud) 我试图用Java解释随机数生成器给朋友,因为他每次运行程序时都会得到相同的数字.我创建了自己的同一个更简单的版本,我也得到了每次运行程序时获得的相同数字.
我究竟做错了什么?
import java.util.*;
public class TestCode{
public static void main(String[] args){
int sum = 0;
Random rand = new Random(100);
for(int x = 0; x < 100; x++){
int num = (rand.nextInt(100)) + 1;
sum += num;
System.out.println("Random number:" + num);
}
//value never changes with repeated program executions.
System.out.println("Sum: " + sum);
}
}
Run Code Online (Sandbox Code Playgroud)
100个中的最后五个数字是:
40
60
27
56
53
Run Code Online (Sandbox Code Playgroud) 这个问题的基础:
今年夏天我将获得CS学位,而不是曾经有一位教授强调Stack的重要性.但是,我有多个项目都专注于递归的使用.我发现递归很有用而令人兴奋,我在个人项目中经常使用它.
我最近去了一个面试,面试官对他们的问题的递归解决方案非常失望.他们想要Stack解决方案.我做了一堆研究,但我仍然不确定何时使用哪种.
鉴于以下演示:
public class TestCode {
static long startTime = 0;
static long stopTime = 0;
static long totalTime = 0;
public static void main(String[] args) throws IOException {
int x = 10000;
startTime = System.currentTimeMillis();
recursiveMethod(x);
System.out.println();
stopTime = System.currentTimeMillis();
totalTime = stopTime - startTime;
System.out.println(totalTime);
System.out.println();
startTime = System.currentTimeMillis();
stackMethod(x);
System.out.println();
stopTime = System.currentTimeMillis();
totalTime = stopTime - startTime;
System.out.println(totalTime);
}
public static void recursiveMethod(int a){
if(a >= 0){
recursiveMethod(a - 1);
System.out.println("Recursion: " + a …Run Code Online (Sandbox Code Playgroud) 我正在开发一个基于画布的应用程序,并且我的文本垂直对齐存在一些问题。我正在使用浮点坐标,我认为这是问题的原因。虽然我不确定,但仍在寻找解决方案。无论如何,例如,我用于在正方形中显示字母“O”的代码是:
context.fillRect(0, 0, 21.864951768488744, 21.864951768488744);
context.textBaseline = 'middle';
context.textAlign = alignLeft ? 'left' : 'center';
context.fillText('O', 10.932475884244372, 10.932475884244372);
Run Code Online (Sandbox Code Playgroud)
画布上的结果是“O”水平居中,但放置在中心上方约 1 - 2 个像素处。
有人对此有什么想法吗?
如何List[Option[Double]]使用以下规则汇总选项列表?
List(Some(1), ..., Some(n)) --> Some(1 + ... + n)List(Some(1), ..., Some(n), None) --> NoneList(None, ..., None) --> None我有 2 个媒体查询大小 -
only screen and (min-width: 980px)and (max-width: 1499px)";
only screen and (min-width: 768px)and (max-width: 979px)";
Safari 正在应用第二个媒体查询,而浏览器宽度仍在第一个媒体查询的范围内 - 当 window.innerwidth 介于 980 - 995 之间时。
我以前从未见过这个 - 有什么想法吗?
更新 -
所以我解决了这个问题 - 新的 OSX safari 的媒体查询使用document.documentElement.clientWidth作为测量 - 而所有其他浏览器(以及我编写的同时启动 JavaScript 的代码)使用window.innerWidth
结果是 safari 因 css 加载的大小而变得混乱,而它不应该为 15 px -
我能找到的唯一解决办法是编写一些 safari 特定的 js - 这并不理想,但我唯一能想到的。
我正在尝试将字符串格式的双精度列表转换为双精度格式.我通过将它们放入列表并使用该Double.parseDouble(string)方法来完成此操作.
这适用于大多数数字,但是当double包含尾随零时,它会提供不需要的输出,parseDouble方法将其删除.我不希望删除它.
String[] values = {"124.50", "45.801", "-15.210"};
List<Double> nums = new ArrayList<Double>();
for(String s: values)
nums.add(Double.parseDouble(s));
Collections.sort(nums);
for(Double d: nums){
System.out.print(d + " ");
}
Run Code Online (Sandbox Code Playgroud)
这产生了输出:
-15.21 45.801 124.5
Run Code Online (Sandbox Code Playgroud)
但我想要尾随的零.使用格式化行的问题是我必须在打印值时指定我想要的浮点精度,但我没有任何特定的愿望使数字准确到某一点,只是留下零点如果他们在那里.
我接近这个错吗?
我有一个具有属性的对象集合,我想删除所有尾随对象(例如)0LINQ中的值.
public class Object
{
public Object(){}
public int Property {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个对象列表:
new Object(){ Property = 1};
new Object(){ Property = 0};
new Object(){ Property = 9};
new Object(){ Property = 7};
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"
Run Code Online (Sandbox Code Playgroud)
如何删除此列表中的"尾随零"?我不想删除所有属性为零的属性,但我想从列表中删除属性值为零的任何对象,如果它后面没有更大的属性值.
我有一个非常简单的选择器,但是当将它添加到 a 时,:not()它似乎不再识别它。
h2:not([random-attribute~="value"] h2){
color: red;
}
[random-attribute~="value"] h2{
color: blue;
}Run Code Online (Sandbox Code Playgroud)
<div class="content">
<h2>Same valid selector, not working</h2>
<div random-attribute="value">
<h2>Valid selector turned blue.</h2>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
据我了解,如果你在里面放了一个有效的选择器,not()你会得到任何不在括号内的h2元素。这是直观的。
不直观的是, 中的选择器在not()单独使用时有效并且可以工作,但是当添加到 中时not()它似乎不起作用。
这不是写这个的有效方法吗?
我在创建散点图的屏幕截图时遇到问题.我的屏幕截图始终包含黑框.
这是我的XAML代码:
<s:SurfaceWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="MakeScreenshots.SurfaceWindow1"
Title="MakeScreenshots" Width="1000" Height="700"
>
<s:SurfaceWindow.Resources>
<ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</s:SurfaceWindow.Resources>
<Grid x:Name="GlobalGrid" Background="{StaticResource WindowBackground}" Width="1000" Height="700" >
<s:ScatterView x:Name="ScatterViewScreenShot" Margin="108,89,176,73" Width="700" Height="500">
<s:ScatterView.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
</s:ScatterView.Background>
<s:ScatterViewItem Margin="0,-26.953,-130.946,-23.047" Content="ScatterViewItem 3" HorizontalAlignment="Right" Width="125.826"/>
<s:ScatterViewItem Margin="0,0,-490.513,-151.256" HorizontalAlignment="Right" Width="125.77" Height="60.427" VerticalAlignment="Bottom" Content="ScatterViewItem 2"/>
<s:ScatterViewItem Content="ScatterViewItem 1" Margin="-331.43,0,0,-129.589" HorizontalAlignment="Left" Width="177.949" Height="67.905" VerticalAlignment="Bottom"/>
</s:ScatterView>
<Button x:Name="MakeScreenShotButton" Click="MakeScreenShotButton_Click" Content="MakeScreenShot" Margin="267,17,343,0" VerticalAlignment="Top" Height="38.96"/>
<Button Content="MakeScreenShotButton2" Height="39" HorizontalAlignment="Left" Margin="116,614,0,0" Name="button1" VerticalAlignment="Top" Width="301" Click="MakeScreenShotButton2_Click" />
<Button …Run Code Online (Sandbox Code Playgroud)