• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struct in java

 
vijayapriya thirumurugan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hello sir,

can i use struct concept in java. if so tell me about that implementation by means of simple example. i'm waiting..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about classes ? Is there anything with classes that does not fit what you consider to be a struct ? (I imagine you're talking about C language)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote: . . . (I imagine you're talking about C language)
He obviously is, yes.

Vijayapriya Thirumurugan: It is important to remember that Java is not C and Java is not an enhancement of C and Java is not C++ and Java is not an enhancement of C++.

Java is a completely different and distinct language.
 
vijayapriya thirumurugan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sir,


i have seen that struct concept is there in java.

it's syntax is:

static class teststruc extends Struct{
String name;
int number;
}


but i didn't have clear idea about this. that's why i asked. but sir, i'll tell my concept using c language. can you clear that in java.

i have struct in c which contains a field which is of array i.e.,

struct test{
int array;
}
b[10][10];

here i am accessing by means of for loop;
i am storing 1 to b[1][1] .array and 2 to b[1][2], etc again 1 to b[2][1].array etc.
can you give this idea in java

 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have struct in c which contains a field which is of array i.e.,

struct test{
int array;
}
b[10][10];


You should be able to simulate this data structure, in Java, using classes -- however, since java classes has to be instantiated (unlike structs in c), it will be annoying to setup.

First, you need to define the test "struct"...



Then you need to declare and initialize the b variable, which is a two dimensional array of the test "struct"...



Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have a struct which is a C/C++ keyword. You have a Struct, which is the name of a Java™ class. You will have to find out where that class came from to get details of it; it's not a J2SE class. Nor is it a struct. But it might be intended to mimic some of the behaviour of a struct.

It is not clear from what you write whether you want an array of Structs, or whether you want a Struct with an int[] field.
 
vijayapriya thirumurugan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i need about array of struct only. can you give me example.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijayapriya thirumurugan wrote:. . . array of struct . . . give me example.
Henry already has given an example.
 
vijayapriya thirumurugan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.*;

public class t {
public static void main(String args[])
{
test b[][] = new test[10][10];
int k=1;
for (int i = 0; i < b.length; i++)
for (int j = 0; j < b[i].length; j++)
{
b[i][j] = new test();
b[i][j].list(new Integer(k));
k++;
}

k=1;
}
}

public class test{
ArrayList li;

public list(int i){
li.add(i);
}
}


i need this output:

0 1 2 3 4 5 6 7 8 9

0 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

1 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

2 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

3 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

4 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

5 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

6 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

7 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

8 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

9 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]


can i get. if not. please give me the code for this output. i'm in running out of time.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++, a struct is more or less the same as a class, but the members are public by default. If you want something similar in Java, you could just create a class with public members:

However, this is not how things are commonly done in Java. If you want something like this, it's better to make a JavaBean out of it, with private member variables and getter and setter methods:
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry already has given an example.


Not only did I gave an example, I gave you the exact code that you need to make the C struct that you provided work.

The issue here is that you changed that C struct, and didn't change the Java counterpart correctly.



You forgot to instantiate the li reference -- and should get a null pointer exception when you call the add() method.

Henry
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijayapriya,

I'd recommend going about this whole thing differently. As opposed to trying to make Java look like C, I'd recommend that you forget about C for the time being and focus on learning Java and the object oriented (OO), concepts that Java uses. Once you have a grasp of OO concepts you can go back to thinking about constructs like "struct" and my guess is that you'll easily come up with OO solutions that are much better than trying to emulate C.

hth,

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic