在二维数组中填入主对角线(Filling in the main diagonals in a 2D array)
我试图编写一个代码来填充NxN矩阵中的2个主要对角线,例如:如果N = 5(通过命令行输入),我们将有一个填充0的5x5矩阵,对角线将有2s填入,如:
2 0 0 0 2 0 2 0 2 0 0 0 2 0 0 0 2 0 2 0 2 0 0 0 2
我写了一个全零表的代码,但是我不知道如何填写对角线。 看看5x5的情况,我将不得不填写以下指数的矩阵:
#1 (0,0) (0,n-1) #2 (1,1) (1,n-2) #3 (2,2) (2,n-3) #4 (3,1) (3, n-2) #5 (4,0) (4,n-1)
但是,由于N可以是任意数字,我假设首先我必须找到中间行,之后我必须以相反的顺序递减索引。
我只学了2周的Java,而这个很难。 我的代码为零填充表是这样的:
public static void main (String[] args){ int n = Integer.parseInt(args[0]); System.out.println(n); int[][] table = new int[n][]; for (int i = 0; i < n; i++) { table[i] = new int[i + 1]; for (int j = 0; j <= i; j++) { table[i][j] = (0); } } System.out.print(Arrays.deepToString(table)); }
显然,这与我所需要达到的目标相去甚远,我不确定它是否完全正确。 我真的很感谢一些帮助。
I am trying to write a code that fills in the 2 main diagonals in an NxN matrix, for example: if N=5 (which is entered through command line), we would have a 5x5 matrix filled with zeros and the diagonals would have 2s filled in, like:
2 0 0 0 2 0 2 0 2 0 0 0 2 0 0 0 2 0 2 0 2 0 0 0 2
I wrote a code for an all-zero table, but i can't figure our how to fill in the diagonals. Looking at the case of 5x5 i would have to fill in the matrix at the following indices:
#1 (0,0) (0,n-1) #2 (1,1) (1,n-2) #3 (2,2) (2,n-3) #4 (3,1) (3, n-2) #5 (4,0) (4,n-1)
However, since N can be any number, i assume that first i have to find the middle row, after which i have to decrement the indices in the reverse order.
I am learning Java for 2 weeks only and this one is pretty hard. My code for a zero-filled table is this:
public static void main (String[] args){ int n = Integer.parseInt(args[0]); System.out.println(n); int[][] table = new int[n][]; for (int i = 0; i < n; i++) { table[i] = new int[i + 1]; for (int j = 0; j <= i; j++) { table[i][j] = (0); } } System.out.print(Arrays.deepToString(table)); }
Obviously, this is very far from what i need to achieve, and i am not sure if it's entirely right. I would really appreciate some help.
原文:https://stackoverflow.com/questions/22204140
最满意答案
简答:不,你不能。
使用交换不是应用程序所做的事情:操作系统的内存管理系统负责这一点。
如果您需要在进程中一次加载超过2GB的数据(而不是根据需要从每个块的磁盘检索数据),那么您就会遇到严重的设计问题。
编辑 :由于您已经在使用64位操作系统,请确保您正在为x64平台(或AnyCPU)编译应用程序,并且您的应用程序未使用WOW64作为32位进程运行。
Short answer: no you can't.
Using the swap is not something applications do: the memory management system of the operating system is responsible of that.
If you need to load more than 2GB of data in your process in one time (and not retrieve data from disk per chunks as necessary), then you have a serious design problem.
EDIT: As you're already using a 64-bit OS, make sure you're compiling your application for x64 platforms (or AnyCPU) and your application is not running as a 32-bit process using WOW64.
相关问答
更多-
我看到的问题是你永远不会读取你保存的数据。 我知道你想通过将这些数据写入文件来保存这些数据......但除非你把它读回去,否则它是没用的。 在您拥有的示例中,当程序启动时...它应该去查找文件calander.txt ,如果发现在现有事件中读取,则允许用户view/edit/delete这些已存在的事件。 当您阅读这些事件时,您可以按原样将它们放入列表中,您可以按原样添加,编辑和删除此列表中的事件。 当用户添加,删除或更新事件时,您将在列表中更新此事件,然后您只需将整个列表写入该文件并覆盖以前的版本。 如果 ...
-
写入C#中的所有位(Write all bits in C#)[2023-09-21]
当你说把所有位都写到文件中时我会假设你的意思是nyble,bit,byte中的位。 那只是给文件写一个整数。 据我所知,你不能拥有一个4位文件,所以最小的面额将是一个字节。 您可能不希望自己负责序列化,因此最简单的选择是使用BinaryReader和BinaryWriter类 ,然后操作C#中的位。 BinaryWriter类最少使用4字节整数。 例如 writer.Write( 1 ); // 01 writer.Write( 10 ); // 0a writer.Write( 100 ); // ... -
如何编写一个类来检索EF和C#中的IEnumerable对象(How to write a Class for retrieving IEnumerable Objects in EF and C#)[2021-04-28]
使用ObjectDataSource而不是代码隐藏。 Use ObjectDataSource instead of codebehind. -
简答:不,你不能。 使用交换不是应用程序所做的事情:操作系统的内存管理系统负责这一点。 如果您需要在进程中一次加载超过2GB的数据(而不是根据需要从每个块的磁盘检索数据),那么您就会遇到严重的设计问题。 编辑 :由于您已经在使用64位操作系统,请确保您正在为x64平台(或AnyCPU)编译应用程序,并且您的应用程序未使用WOW64作为32位进程运行。 Short answer: no you can't. Using the swap is not something applications do: th ...
-
我不认为这是一个讨论这类问题的好地方,它是一个与节目相关的网站, 我可以给你一个提示,这应该没问题,它甚至可以加快你的分页文件,下面的一些参考(我实际上是最近读它:)): http://lifehacker.com/5426041/understanding-the-windows-pagefile-and-why-you-shouldnt-disable-it 以下部分: 您实际应该做的是将页面文件移动到完全不同的物理驱动器以分离工作负载。 但与一些Windows管理专家确认 I dont think t ...
-
资源监视器使用c:\ pagefile.sys显示sqlserver.exe(Resource Monitor Shows sqlserver.exe using c:\pagefile.sys)[2022-11-02]
SQL Server遇到64357次发生的I / O请求超过15秒 不表示存在内存问题。 它表示用于存储数据库的磁盘存在问题。 SQL Server has encountered 64357 occurrence(s) of I/O requests taking longer than 15 seconds Does not indicate a memory problem. It indicates a problem with the disks used to store your databa ... -
PowerShell脚本设置pagefile.sys的大小[关闭](PowerShell Script to set the size of pagefile.sys [closed])[2022-06-10]
这是我们如何通过PowerShell更新pagefile.sys的大小: # PowerShell Script to set the size of pagefile.sys $computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges; $computersys.AutomaticManagedPagefile = $False; $computersys.Put(); $pagefile = Get-WmiObject ... -
实际上, XmlSerializer可以在序列化时流入和流出可枚举的数据。 它对实现IEnumerable
的类有特殊处理。 来自文档 : XmlSerializer为实现IEnumerable或ICollection的类提供特殊处理。 实现IEnumerable的类必须实现一个带有单个参数的公共Add方法。 Add方法的参数必须与从GetEnumerator返回的值的Current属性返回的类型相同,或者是该类型的base之一。 在序列化这些类时, XmlSerializer只是遍历可枚举的每个当前值 ... -
如果要在整个图像中进行过滤,则应考虑锁定位图,将图像数据复制到数组,然后同时更新数组数据而不是调用SetPixel 。 If you are filtering in the whole image you should consider locking the bitmap, copying the image data to an array and then concurrently updating the array data instead of calling SetPixel.
-
在C#中修改对象(Modifying Objects in C#)[2022-09-07]
如果你使TData成为一个结构,那么它将按值复制而不是通过引用复制。 否则你将不得不制作一个复制值的浅层克隆。 If you make TData a struct then it will be copied by value and not by reference. Otherwise you'll have to make a shallow clone which copies values.