|
yeah, i want to know how to calculate the length of 2d array while converting it into 1d array
when i simply used a.length for my 2d array and put it into my 1d array ,then 1d array will print only first column then exception comes Array index out of bound
|
|
|
|
|
Well I would guess it is the total number of elements in your 2D array. So it is just a question of adding the number of elements in array[0] + array[1] + ...
|
|
|
|
|
yup i got the idea and it works like this
int a[][]={{1,2,3},{4,5,6},{7,2,6}};
int b[] = new int[a.length*a.length];
System.out.print(b.length);
//output is 9
//is this the way of getting length of 2d into 1d array?
|
|
|
|
|
What happens if you have something like:
int a[][]={{1,2,3,7,12},{4,5},{7,2,6,9,9,11,22,33,44,55}};
|
|
|
|
|
yup in that case my logic isn't working
if you can, please tell me the universal logic for calculating the length while converting 2d array into 1d array
|
|
|
|
|
|
ok i got the answer
thanks buddy for help
|
|
|
|
|
This is showing error
So i solved from my self.....
class Sort1{
public static void main(String...avg)
{
int a[][]={{10,50,40},{15,25,10},{25,14,19}};
int l =a.length;
int b[]=new int[9];
int x=0;
for(int i=0;i<a.length;i++)
{
for(int j = 0;j<a[i].length;j++)
{
System.out.print(a[i][j]+"\t");
b[x]=a[i][j];
x++;
}
System.out.println();
for(int k=0;k<b.length;k++)
{
System.out.print(b[k] +"\t");
}
}
}
|
|
|
|
|
Basically i was trying to do bubble sorting in 2d array with logic
first converted 2d array into 1 d array then sort the elements and again converted back that array into 2d array
class Sort1
{
public static void main(String...avg)
{
int a[][]={{1,2,3,7,12},{4,5},{7,2,6,9,9,11,22,33,44,55}};
//System.out.print(a.length);
int totalelements=0;
for(int x=0;x<a.length;x++)
{
for(int y=0;y<a[x].length;y++)
{
totalelements++;
}
}
//System.out.print(totalelements);
System.out.println("\n"+"unsorted 2d array"+"\n");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n"+"converted 2d array into 1d array"+"\n");
int b[]=new int[totalelements];
//System.out.print(b.length);
int l=b.length;
int c=0;
for(int k=0;k<a.length;k++)
{
for(int m=0;m<a[k].length;m++)
{
b[c]=a[k][m];
System.out.print(b[c] +"\t");
c++;
}
}
for( int i =1; i<=l-1;i++)
{
for(int j=0; j<l-i;j++)
{
if(b[j]>b[j+1])
{
c =b[j];
b[j]=b[j+1];
b[j+1]=c;
}
}
}
int n=0;
for(int i=0 ; i<a.length ; i++)
{
for(int j=0 ; j<a[i].length ; j++)
{
a[i][j] = b[n];
n++;
}
}
System.out.println("\n"+"\n"+"Sorted 2d array "+"\n");
for(int i=0 ; i<a.length ; i++)
{
for(int j=0 ; j<a[i].length ; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
|
|
|
|
|
Hi!
I am currently coding in JSF, but using Java for the back-end beans. Was wondering if anyone is familiar to JSF, to help get a solution or an idea on how I could proceed.
So, if I have a database with 2 tables - Country & State,
but I don't want to have to make a call into the database in order to reduce the resources used. How would I implement in the Java bean a connection between the country and its related states?
Considering that I have the 2 selections in a dropdown menu.
Thanks!
|
|
|
|
|
If you do not want to use the database then you need to hold all the data in Lists inside the code, which may be just as expensive.
|
|
|
|
|
And if I make a call to the database, then I'm probably looking to use a couple of for-loops
to go through both tables in my back bean?
|
|
|
|
|
Yes, probably. It's a trade off, whichever method you use will have a cost. You need to decide which is the better option for your application.
|
|
|
|
|
a very beginner question...
i'm a newbie in programming... just started java and read about packaging... i created i directory called firstproject and put a directory with one class inside of it and a FirstProject source file in the firstproject directory itself.
i made a class in that package directory that has a private string in it and tried to invoke from FirstProject source file but
getting compile error that the string is private...
i tried changing the string into protected or even public but still getting this error...
this is the class src file...
<pre lang="java">
package stickers;
public class Sticker
{
private String note;
Sticker(String note)
{
this.note = note;
}
public void displayNote()
{
System.out.println(note);
}
}
this is the FirstProject src file...
package firstproject;
import stickers.Sticker;
public class FirstProject
{
public static void main(String[] args)
{
Sticker sticker = new Sticker("hello sticky mehdi");
sticker.displayNote();
}
}
this is the actuall error:
error: Sticker(String) is not public in Sticker; cannot be accessed from outside package
|
|
|
|
|
You need to make your constructor public so other packages can access it:
public class Sticker
{
private String note;
public Sticker(String note)
{
this.note = note;
}
I would recommend going to The Java Tutorials[^] and working through them.
|
|
|
|
|
Hello there. I have managed to load and use c++ dll using JNI, in my java servlet (System.load). It works perfectly on first request I submit. But on subsequent requests, I get this weir d exception.
java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/XXXXXX.dll already loaded in another classloader<br />
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525)
Here is my loading code
public void init(ServletConfig config) throws ServletException
{
super.init(config);
ServletContext context = getServletContext();
String sMathDllpath = context.getInitParameter("MathDLLPath");
System.load(sMathDllpath);
}
I studied this tomcat wiki link and implemented their solution, among others, but could not succeed.
How do I implement it successfully? Thnks for anything you share.
|
|
|
|
|
You should declare it as a static action at class level like:
class ... {
static
{
System.loadLibrary("MathDLLPath");
}
}
That will ensure it is loaded when the class is first instantiated, but will only be loaded once.
|
|
|
|
|
Richard MacCutchan wrote: You should declare it as a static action at class level like:
I tried this just now. This is what I have
public class MainServlet extends HttpServlet
{
static{
System.loadLibrary("MathDll");
}
.........
}
And this is the exception I am getting
<br />
java.lang.UnsatisfiedLinkError: Native Library C:\Program Files\Java\jdk1.7.0_71\bin\MathDll.dll already loaded in another classloader<br />
What am I doing wrong now?
|
|
|
|
|
No idea, but it looks like you may have more than one call somewhere. Time to get the debugger out. Although, I have never used JNI in a servlet so it may be that there is something else happening.
|
|
|
|
|
Hello, I wanted to create a launcher for a game (Minecraft), but then I had an idea, that if I read some source codes that it would make it easier for me to make it, so I went searching for a source code , and I found one, but then I was testing it if it's working or not, and it opened, but then when I pressed launch after seconds it says that it couldn't update or something, I'm gonna put the source code file if you could please help me I'll appreciate that.
SKMCLauncher-fourth[^]
The error is
The following information can be provided to the developer:
com.sk89q.skmcl.LauncherException: Something went wrong while trying to update.
at com.sk89q.skmcl.launch.LaunchWorker.update(LaunchWorker.java:105)
at com.sk89q.skmcl.launch.LaunchWorker.call(LaunchWorker.java:141)
at com.sk89q.skmcl.launch.LaunchWorker.call(LaunchWorker.java:44)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "downloads" (Class com.sk89q.skmcl.minecraft.model.Library), not marked as ignorable
at [Source: java.io.StringReader@6738ae66; line: 21, column: 27] (through reference chain: com.sk89q.skmcl.minecraft.model.ReleaseManifest["libraries"]->com.sk89q.skmcl.minecraft.model.Library["downloads"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at com.sk89q.skmcl.util.HttpRequest$BufferedResponse.asJson(HttpRequest.java:470)
at com.sk89q.skmcl.minecraft.MinecraftUpdater.installGame(MinecraftUpdater.java:166)
at com.sk89q.skmcl.minecraft.MinecraftUpdater.call(MinecraftUpdater.java:129)
at com.sk89q.skmcl.minecraft.MinecraftUpdater.call(MinecraftUpdater.java:53)
at com.sk89q.skmcl.launch.LaunchWorker.update(LaunchWorker.java:101)
... 6 more
|
|
|
|
|
Which area of Java has the greatest potential? Java security, web/mobile, telecommunications or data analytics?
|
|
|
|
|
Which area of Java has the greatest potential? Java security, web/mobile, telecommunications or data analytics?
|
|
|
|
|
None of us can foretell the future.
|
|
|
|
|
Security in any field will always be big, in my opinion.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi,
I have a Oracle DB in which all the information is stored and this information is been consumed by different teams and below is the two options i have to decide on :
1. Write a Stored procedure and give the stored procedure to the respective teams and they can call the stored procedure.
2. Write a stored procedure wrap it as an API and expose the API, different team will call the API exposed and API will in turn hit the SP and return the response to the team.
Like to know what is the PRON and CRONS with these options and what is the best possible solution to go with.
Thanks
|
|
|
|